将TreeView控件数据绑定到视图模型 [英] DataBinding a TreeView Control to a View Model
问题描述
大家好,
我是整个WPF领域的新手,如果有人可以向我解释一些事情,我将非常感激.我有一个绑定到视图模型对象的树视图.只需填充树形视图即可.我遇到问题的地方是何时将数据保存在表单上,并且树形视图刷新".如果用户具有展开的树视图的特定节点,则当树视图刷新"时,该节点折叠.保存后.
我将视图模型对象放在适当的位置,以便可以向其添加IsExpanded属性,然后在节点上进行两种方式绑定,以便在树视图刷新时该节点保持扩展状态.当前它不起作用,我认为为什么不能正确地对IsExpanded属性进行数据绑定.
这里是有问题的XAML:
Hello All,
I'm sort of new to the whole WPF scene and would be most thankful if someone could explain something to me. I have a treeview that is bound to a view model object. The treeview is populated just fine. Where I'm running into a problem is when I save the data on the form and the treeview "refreshes". If the user has a particular node of the treeview expanded that node collapses when the treeview "refreshes" upon a save.
I put the view model object in place so I could add an IsExpanded property to it and then do a two way bind on the node so that the node would stay expanded when the treeview refreshes. It currently doesn't work and I think the reason why is I am not databinding the IsExpanded property properly.
Here is the XAML in question:
<TreeView Name="trvModules" Grid.Column="0" DataContext="{Binding Source = {StaticResource UserModuleList}}" ItemsSource="{Binding Path=Model}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding UserModules}">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="1,1,5,1"
IsChecked="{Binding Path=HasModuleAccess, Converter={StaticResource IdentityConverter}, Mode=TwoWay}"
Checked="chkModules_Checked"
Name="chkModules" />
<TextBlock Style="{DynamicResource TextBlock_MouseOverTrigger_Style}"
Text="{Binding Path=DisplayName}"
ToolTip="{Binding Path=ModuleDescription}">
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
推荐答案
凯文,您好,
您能在一个简单的项目中重现您的问题并将其发送给我吗?我的电子邮件地址是microsoft dot com上的v-lliu. />论坛中的MSDN订户支持
如果您对我们的支持有任何反馈意见,请联系 msdnmg@microsoft.com
Hi Kevin,
Could you please reproduce your problem in a simple project and send it to me? My email address is v-lliu at microsoft dot com.
I look forward to your reply!
Sincerely,
Linda Liu
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
这篇关于将TreeView控件数据绑定到视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!