2016-09-23 10 views
-2

質問:どのようにWebBrowserのイベントをItemTemplateのビューモデルのICommandプロパティにバインドするのですか?WPFのTabItemのWebBrowser、MVVMのDataTemplateでのイベントのバインド

エラーコレクションプロパティ 「Microsoft.VisualStudio.DesignTools.WpfDesigner.InstanceBuilders.HwndHostInstance」:私はMvvmLightの道のために、通常ではExpression Blendの対話性ライブラリ、exeptionのocursを使用して、これを行うにしようとしています

'トリガー' はnullです。

WebTabItems項目のviewmodels

の監視可能なコレクションは、ここでは、コードです:タブコントロールにバインド

<TabControl ItemsSource="{Binding WebTabItems}" SelectedItem="{Binding SelectedWebTabItem}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" > 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="SelectionChanged"> 
         <mvvm:EventToCommand Command="{Binding SelectionChangedVMCommand}" PassEventArgsToCommand="True"></mvvm:EventToCommand> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 

       <TabControl.ItemTemplate> 
        <!--header--> 
        <DataTemplate> 
         <TextBlock Text="{Binding Header}"></TextBlock>        
        </DataTemplate>       
       </TabControl.ItemTemplate> 
       <TabControl.ContentTemplate> 
        <DataTemplate>       
         <Grid > 
          <Grid.RowDefinitions> 
           <RowDefinition Height="auto"></RowDefinition> 
           <RowDefinition></RowDefinition> 
          </Grid.RowDefinitions> 

          <TextBlock Text="{Binding NotificationRibbonText}" Visibility="{Binding NotificationRibbonVisibility}"></TextBlock> 


          <WebBrowser Grid.Row="1" Visibility="Visible" local:WebBrowserExtension.BindableSource="{Binding Sourse}" > 
           <i:Interaction.Triggers> 
            <i:EventTrigger EventName="Navigating"> 
             <mvvm:EventToCommand Command="{Binding NavigatingMVCommand}" PassEventArgsToCommand="True" ></mvvm:EventToCommand> 
            </i:EventTrigger> 
           </i:Interaction.Triggers> 
          </WebBrowser> 



         </Grid> 
        </DataTemplate> 
       </TabControl.ContentTemplate> 

      </TabControl> 

イベントは素晴らしい作品が、テンプレートにPsは可能かもしれない

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
            <i:Interaction.Triggers> 
             <i:EventTrigger EventName="Navigating"> 
              <mvvm:EventToCommand Command="{Binding NavigatingMVCommand}" PassEventArgsToCommand="True" ></mvvm:EventToCommand> 
             </i:EventTrigger> 
            </i:Interaction.Triggers> 

を行います問題はWebBrowser.Navigatingはルーティングされたイベントではありませんが、問題は同じです、どのようにイベントにバインドするのですか?

答えて

0

しているが、どのようにあるExpression Blendの対話性ライブラリを使用している場合、テンプレートでイベントをバインドするかの答えをfoud不可能

のICommand型の

添付プロパティは、同じ機能を実現できるような他の方法です。

この回答もありませんルーティングイベント私の場合は

に結合するために使用することができます。オリジナルのWebブラウザに添付依存プロパティを追加する XAML

<WebBrowser Grid.Row="1" Visibility="Visible" 
             local:WebBrowserExtension.BindableSource="{Binding NavigeteToSourse}" 
             local:WebBrowserExtension.NavigatingCmdExtended="{Binding NavigatingMVCommand}" 

             > 

          </WebBrowser> 

分離クラス

class WebBrowserExtension 
    { 

    #region BindableSourceProperty 

    public static readonly DependencyProperty BindableSourceProperty = 
    DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserExtension), new UIPropertyMetadata("", BindableSourcePropertyChanged)); 

    public static string GetBindableSource(DependencyObject obj) 
    { 
     return (string)obj.GetValue(BindableSourceProperty); 
    } 

    public static void SetBindableSource(DependencyObject obj, string value) 
    { 
     obj.SetValue(BindableSourceProperty, value); 
    } 

    public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) 
    { 
     WebBrowser browser = o as WebBrowser; 
     if (browser != null) 
     { 
      string uri = e.NewValue as string; 
      browser.Source = !String.IsNullOrEmpty(uri) ? new Uri(uri) : null; 
     } 
    } 
    #endregion 

     #region NavigatingCmdExtended 

     public static ICommand GetNavigatingCmdExtended(DependencyObject obj) 
     { 
      return (ICommand)obj.GetValue(NavigatingCmdExtendedProperty); 
     } 
     public static void SetNavigatingCmdExtended(DependencyObject obj, ICommand value) 
     { 
      obj.SetValue(NavigatingCmdExtendedProperty, value); 
     } 
     // Using a DependencyProperty as the backing store for CalenderOpenCommand. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty NavigatingCmdExtendedProperty = 
     DependencyProperty.RegisterAttached("NavigatingCmdExtended", typeof(ICommand), typeof(WebBrowserExtension), new PropertyMetadata(OnChangedNavigatingCmdExtendedProperty)); 


     private static void OnChangedNavigatingCmdExtendedProperty(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      var webBrowser = d as WebBrowser; 
      if (webBrowser != null) 
      { 
       if (e.NewValue != null) 
       { 
        //attach event handler 
        webBrowser.Navigating += webBrowser_Navigating; 
       } 
       else 
       { 
        //detach event handler 
        webBrowser.Navigating -= webBrowser_Navigating; 
       } 
      } 
     } 
     /// 
     /// Event handler for Calender Opened event. 
     /// 
     /// 
     /// 
     static void webBrowser_Navigating(object sender, NavigatingCancelEventArgs e) 
     { 
      ICommand command = GetNavigatingCmdExtended(sender as DependencyObject); 
      if (command != null) 
      { 
       if (command.CanExecute(e)) 
       { 
        //executes a command 
        command.Execute(e); 
       } 
      } 
     } 
     #endregion 

およびViewModelのコマンド

public class WebTabItemVievModel: ViewModelBase 
{ 

    public WebTabItemVievModel() 
    { 

     NavigatingMVCommand = new RelayCommand<NavigatingCancelEventArgs>(NavigatingMethod); 

    } 



    public ICommand NavigatingMVCommand { get; set; } 
    private void NavigatingMethod(NavigatingCancelEventArgs e) 
    { 
     Messenger.Default.Send<NotificationMessage <UriChangedMSG>>(new NotificationMessage<UriChangedMSG> (new UriChangedMSG { NewUri = e.Uri.AbsoluteUri },"test")); 
     CurrentUri = e.Uri.AbsoluteUri; 
     NotificationRibbonText = e.Uri.AbsoluteUri; 
    } 

詳細はこちらの記事を参照してください:

http://www.codeproblem.com/articles/frameworks/wpf/87-event-to-command-binding-using-attached-properties-in-plain-wpf-without-any-extra-dependancy?showall=1&limitstart=

関連する問題