我尝试将 galasoft mvvm-light 框架与我的 wpf treeviev 控件一起使用,但在相应的视图模型中不会调用任何命令方法。
视图模型:
public class TreeViewViewModel : WorkspaceViewModel
{
private List<TreeItemViewModel> _items;
public List<TreeItemViewModel> Items { get { return _items; } set { _items = value; } }
public RelayCommand<RoutedPropertyChangedEventArgs<object>> SelectedItemChangedCommand { get; private set; }
public RelayCommand<EventArgs> MouseDoubleClickCommand { get; private set; }
public TreeViewViewModel()
{
SelectedItemChangedCommand = new RelayCommand<RoutedPropertyChangedEventArgs<object>>(e => OnSelectedItemChanged(e));
}
void OnSelectedItemChanged(RoutedPropertyChangedEventArgs<object> e)
{
var xy = e;
}
}
看法:
<TreeView ItemsSource="{Binding Items}" Grid.Row="1" x:Name="trvTemplate">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Text}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<cmd:EventToCommand Command="{Binding SelectedItemChangedCommand, Mode=OneWay}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cmd:EventToCommand Command="{Binding MouseDoubleClickCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TreeView>
我已经阅读了一些博客,它应该可以工作,但我知道不是调用了命令。如果有更好或更简单的方法将 wpf treeview 与 mvvm 模式结合起来,请给我相关信息。