我正在使用最新版本的mvvm light工具包,但是我不清楚如何将EventToCommand用于事件TreeViewItem.Expanded。
这很重要……我在做什么错?
<TreeView Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Path= MonitoredDatabases}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Queues}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ServerName}" />
<TextBlock Text="\" />
<TextBlock Text="{Binding DatabaseName}" />
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding QueueName}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="TreeViewItem.Expanded">
<cmd:EventToCommand Command="{Binding Path=NodeExpanded}"
CommandParameter="Expanded" />
</i:EventTrigger>
<i:EventTrigger EventName="TreeViewItem.Collapsed">
<cmd:EventToCommand Command="{Binding Path=NodeCollapsed}"
CommandParameter="Collapsed" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TreeView>
帮助非常感谢。
问候。
加里
最佳答案
我可以通过为TreeView创建自定义ItemContainerStyle来做到这一点。您应该能够将其与以下代码段放在一起。
1. TreeView的ViewModel
public class TreeViewModelView
{
public IEnumerable<object> Values
{
get { /* return hierarchical data source here ... */ }
}
/// <summary>
/// Command executed when the TreeViewItem expanding event is raised. The data item is passed in as a parameter.
/// </summary>
public RelayCommand<object> ExpandedCommand
{
get { return new RelayCommand<object>( o => MessageBox.Show( o.GetType().Name ) ); }
}
/// <summary>
/// Command executed when the TreeViewItem collapsing event is raised.
/// </summary>
public RelayCommand CollapsedCommand
{
get { return new RelayCommand( () => MessageBox.Show( "Collapsed" ) ); }
}
}
2. 定义TreeView并设置所需的数据绑定(bind):
<TreeView x:Name="lstItems" HorizontalAlignment="Left" Margin="21,19,0,80" Width="283"
DataContext="{DynamicResource TreeViewModelView}"
ItemsSource="{Binding Values, Mode=OneWay}"
ItemTemplate="{DynamicResource TreeViewDataTemplate}"
ItemContainerStyle="{DynamicResource TreeViewItemStyle}">
<TreeView.Resources>
<mv:TreeViewModelView x:Key="TreeViewModelView" />
<HierarchicalDataTemplate x:Key="TreeViewDataTemplate" ItemsSource="{Binding Items}">
<Grid>
<TextBlock Text="{Binding Name}" />
</Grid>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
3. 。在Expression Blend中,您可以创建编辑ItemContainerStyle模板的副本。
<Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
...
</Style>
4. 然后将ToggleButton替换为:
<ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}">
<i:Interaction.Triggers>
<!-- When the Checked event is raised execute the ExpandedCommand with the data item as a parameter. -->
<i:EventTrigger EventName="Checked">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=DataContext.ExpandedCommand, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, Mode=OneWay}"
CommandParameter="{Binding}" />
</i:EventTrigger>
<!-- When the Unchecked event is raised execute the CollapsedCommand. -->
<i:EventTrigger EventName="Unchecked">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=DataContext.CollapsedCommand, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ToggleButton>
关于wpf - TreeViewItem.Expanded,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3255285/