【发布时间】:2015-05-15 22:15:48
【问题描述】:
我有一个带有 TextBlock 的 WPF 分层 TreeView 来显示我的列表。我想将选定的字符串值作为参数传递给我的 ViewModel。我正在使用 MVVM。
这是树视图:
<TreeView ItemsSource="{Binding countryReportsHierarchy}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<i:InvokeCommandAction Command="{Binding ArticleCategoryTitleSelectionChangedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type t:CountryReportsHierarchy}"
ItemsSource="{Binding ArticleCategoriesHierarchyCollection}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type t:ArticleCategoriesHierarchy}"
ItemsSource="{Binding ArticleTypesHierarchyCollection}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type t:ArticleTypesHierarchy}">
<TextBlock Text="{Binding ElementName= Name, Path=SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
</TextBlock>
</DataTemplate>
</TreeView.Resources>
</TreeView>
我试图从 TextBlock 中获取选定值的部分来自:
<TextBlock Text="{Binding Name}"/>
这按预期绑定,显然没有选择值。有没有办法传递 TextBlock 的选定值?我尝试了以下方法,但绑定失败:
<TextBlock Text="{Binding ElementName= Name, Path=SelectedItem" />
【问题讨论】:
-
听起来不对,给我们看更多代码
-
您想要整个 TreeView 代码?
-
只有2个数据模板,你在这里显示的第一个在第一行本身就关闭了
-
文本块中没有SelectedItem,因为它不是itemscontrol
-
那么你需要一个行为,看看 [这里][1] 的示例。 [1]:stackoverflow.com/questions/11065995/…