MVVM Light EventToCommand - 无法让evnt触发

时间:2015-11-03 19:58:50

标签: c# wpf mvvm

我已经阅读了论坛上为EventToCommand提供的每个答案,并且无法触发我的事件。以下是构成我的问题的代码片段。我使用的是Visual Studio 2015社区版。

UserControl声明:

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>

我的ViewModel逻辑:

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");
            }
        }
    }

我知道我必须遗漏一些东西。有人可以帮忙吗?

感谢-你。

Dave K

2 个答案:

答案 0 :(得分:0)

@Rachel是对的

您正在使用MvvmLight,因此请使用内置的relay命令。将它放在您的视图模型中并绑定到公共属性 MyCommand 或您重命名为的任何内容。

public RelayCommand MyCommand 
{ 
  get; 
  private set; 
} 

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

private void ExecuteMyCommand() 
{ 
  // Do something 
}

您需要做一些阅读以确保您理解这一点,因为您不应该在MVVM中使用控制事件来处理VM相关功能

看看HERE

答案 1 :(得分: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>