2015-11-03 102 views
2

我已閱讀關於EventToCommand論壇上提供的每個答案,無法讓我的事件觸發。這是構成我的問題的代碼片段。我正在使用Visual Studio 2015社區版。MVVM Light EventToCommand - 無法獲取evnt觸發

用戶控件聲明:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
xmlns:cmd="http://www.galasoft.ch/mvvmlight" 


<UserControl.DataContext> 
    <vm:MenuTreeViewModel></vm:MenuTreeViewModel> 
</UserControl.DataContext> 

<syncfusion:TreeViewAdv x:Name="treeMain" Width="340" Margin="10,0,0,0"  
    ItemsSource="{Binding Collection, Mode=TwoWay, 
    UpdateSourceTrigger=PropertyChanged}" Grid.ColumnSpan="1" 
           VisualStyle="ShinyBlue" FontFamily="Verdana" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Top" VerticalAlignment="Top" 
           HorizontalAlignment="Left" FontSize="11" Height="768" 
           ScrollViewer.VerticalScrollBarVisibility="Auto" Visibility="Visible"> 

<i:Interaction.Triggers> 
    <i:EventTrigger EventName="SelectedItemChanged"> 
     <cmd:EventToCommand Command="{Binding DataContext.SelectedItemChanged, 
            ElementName=treeMain, Mode=OneWay}" 
            PassEventArgsToCommand="True" /> 
    </i:EventTrigger> 
</i:Interaction.Triggers> 

我的視圖模型邏輯:

MenuTreeViewModel代碼:

public void SelectedItemChanged(object sender, 
            RoutedPropertyChangedEventArgs<object> e) 
     { 
      if (e.NewValue is Models.MenuTree) 
      { 
       // This is the Top Level Clients item 
       // Nothing needs to be done. 
       // clear out Current items of all Model types 
       // MenuTree tree = (MenuTree)e.NewValue; 

      } 
      if (e.NewValue is Models.Provider) 
      { 
       //MessageBox.Show("Provider"; 
       // Set CurrentProvider to the selected item. 
       Dal db = new Dal(); 
      } 
      if (e.NewValue is Models.Batch) 
      { 
       MessageBox.Show("Batch"); 
      } 
      if (e.NewValue is Models.Consumer) 
      { 
       MessageBox.Show("Consumer"); 
      } 
     } 
    } 

我知道我必須失去了一些東西。有人可以幫忙嗎?

謝謝你。

戴夫ķ

+3

我不認爲你可以綁定到這樣的默認方法....你需要一個'ICommand'運行你的方法。 – Rachel

+0

就像@Rachel說的,你只能綁定一個'ICommand'。因此,名稱,事件*命令*。 – Andro

回答

0

@Rachel是正確

您使用MvvmLight所以使用內置繼電器命令。將其放入您的視圖模型中,並綁定到公共屬性MyCommand或您將其重命名爲的任何內容。

public RelayCommand MyCommand 
{ 
    get; 
    private set; 
} 

public MainViewModel() 
{ 
    MyCommand = new RelayCommand( 
    ExecuteMyCommand, 
    () => _canExecuteMyCommand); 
} 

private void ExecuteMyCommand() 
{ 
    // Do something 
} 

你需要做一些閱讀,以確保你明白這一點,你不應該使用虛擬機相關的功能的控制事件MVVM

看看HERE

0

可以使用不調用方法EventToCommand,該屬性應該是一個命令。

你可以嘗試以下,使其工作

MenuTreeViewModel.cs

public class MenuTreeViewModel 
{ 
    public ICommand SelectedValueUpdated { get; set; } 

    public MenuTreeViewModel() 
    { 
      SelectedValueUpdated = new SelectedValueUpdated(this); 
    } 
} 

SelectedValueUpdated.cs

class SelectedValueUpdated : ICommand 
{ 
    private MenuTreeViewModel _mvModel; 
    public event EventHandler CanExecuteChanged 
    { 
     add { } 
     remove { } 
    } 
    public SelectedValueUpdated(MenuTreeViewModel mvModel) 
    { 
     _mvModel = mvModel; 
    } 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public void Execute(object parameter) 
    { 
     RoutedPropertyChangedEventArgs<object> e = (RoutedPropertyChangedEventArgs<object>)parameter; 
     if (e.NewValue is Models.MenuTree) 
     { 
      // This is the Top Level Clients item 
      // Nothing needs to be done. 
      // clear out Current items of all Model types 
      // MenuTree tree = (MenuTree)e.NewValue; 

     } 
     if (e.NewValue is Models.Provider) 
     { 
      //MessageBox.Show("Provider"; 
      // Set CurrentProvider to the selected item. 
      Dal db = new Dal(); 
     } 
     if (e.NewValue is Models.Batch) 
     { 
      MessageBox.Show("Batch"); 
     } 
     if (e.NewValue is Models.Consumer) 
     { 
      MessageBox.Show("Consumer"); 
     } 
    } 
} 

UserControlDeclarations

<syncfusion:TreeViewAdv x:Name="treeMain" Width="340" Margin="10,0,0,0"  
     ItemsSource="{Binding Collection, Mode=TwoWay, 
     UpdateSourceTrigger=PropertyChanged}" Grid.ColumnSpan="1" 
            VisualStyle="ShinyBlue" FontFamily="Verdana" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Top" VerticalAlignment="Top" 
            HorizontalAlignment="Left" FontSize="11" Height="768" 
            ScrollViewer.VerticalScrollBarVisibility="Auto" Visibility="Visible"> 

    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="SelectedItemChanged"> 
      <cmd:EventToCommand Command="{Binding DataContext.SelectedValueUpdated, Mode=OneWay}" PassEventArgsToCommand="True" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
    </syncfusion:TreeViewAdv> 
相關問題