gpt4 book ai didi

wpf - TreeViewItem.Expanded

转载 作者:行者123 更新时间:2023-12-03 10:18:49 24 4
gpt4 key购买 nike

我使用最新版本的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com