使用绑定到 XDocument 的 DataContext 在 WPF TreeView 中设置 selectedItem

伯恩德·舒马赫

我的 WPF 窗口中有 TreeView。TreeViews DataContext 绑定到 XDocument。

我正在寻找从代码后面设置 selectedItem 。由于例如 .ItemContainerGenerator.Items 返回的节点是 System.Xml.Linq.XElement 类型而不是 System.Windows.Controls.TreeViewItem 我不能只设置 isSelected :-(

有人知道如何解决这个问题吗?

编辑:XAML 代码:

<Window.Resources>
    <DataTemplate x:Key="AttributeTemplate">
        <StackPanel Orientation="Horizontal"
        Margin="3,0,0,0"
        HorizontalAlignment="Center">
            <TextBlock Text="{Binding Path=Name}"
         Foreground="{StaticResource xmAttributeBrush}" FontFamily="Consolas" FontSize="8pt" />
            <TextBlock Text="=&quot;"
         Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
            <TextBlock Text="{Binding Path=Value, Mode=TwoWay}"
         Foreground="{StaticResource xmlValueBrush}" FontFamily="Consolas" FontSize="8pt" />
            <TextBlock Text="&quot;"
         Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
        </StackPanel>
    </DataTemplate>

    <HierarchicalDataTemplate x:Key="NodeTemplate">
        <StackPanel Orientation="Horizontal" Focusable="False">
            <TextBlock x:Name="tbName" Text="Root" FontFamily="Consolas" FontSize="8pt" />
            <ItemsControl
        ItemTemplate="{StaticResource AttributeTemplate}" HorizontalAlignment="Center"
        ItemsSource="{Binding Converter={StaticResource AttrConverter}}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </StackPanel>
        <HierarchicalDataTemplate.ItemsSource>
            <Binding Path="Elements" />
        </HierarchicalDataTemplate.ItemsSource>
        <HierarchicalDataTemplate.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Path=NodeType}" Value="Element" />
                    <Condition Binding="{Binding Path=FirstNode.FirstNode.NodeType}" Value="Text" />
                </MultiDataTrigger.Conditions>
                <Setter TargetName="tbName" Property="Text" >
                    <Setter.Value>
                        <Binding Path="Name" />
                    </Setter.Value>
                </Setter>
                <Setter TargetName="tbName" Property="ToolTip">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource MultiString2StringKonverter}">
                            <Binding Path="FirstNode.Value" />
                            <Binding Path="FirstNode.NextNode.Value" />
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </MultiDataTrigger>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=FirstNode.NodeType}" Value="Text">
                <Setter TargetName="tbName" Property="Text">
                    <Setter.Value>
                        <MultiBinding StringFormat="{}{0} = {1}">
                            <Binding Path="Name"/>
                            <Binding Path="FirstNode.Value"/>
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </HierarchicalDataTemplate.Triggers>
    </HierarchicalDataTemplate>
</Window.Resources>

<TreeView x:Name="LizenzAnsicht"
      ItemsSource="{Binding Path=Root.Elements, UpdateSourceTrigger=PropertyChanged}"
      ItemTemplate="{StaticResource ResourceKey=NodeTemplate}"
      />

背后的代码:...

LizenzAnsicht.DataContext = XDocument.Load(<Path to XML-File>);
伯恩德·舒马赫

在睡了一个周末之后,我自己找到了答案。还有一些问题,因为只有返回类型。

解决方案是,为 Item 获取匹配的 ItemContainer。这个容器是 TreeViewItem 类型,我可以在其中设置 isSelected 等。

作为一个例子在大多数情况下更好地理解,我举一个。以下函数将在 TreeView- 对象中搜索 XML- 节点并返回包含匹配节点的 TreeViewItem。如果未找到匹配项,则返回 null。匹配节点被扩展。XML 在绑定到 System.Windows.Controls.TreeView DataContext 的 System.Xml.linq.XDocument 中处理。调用函数时TreeView设置为ic。

public static TreeViewItem SearchNodeInTreeView(ItemsControl ic, XElement NodeDescription, string indent = "")
{
    TreeViewItem ret = null;
    foreach (object o in ic.Items)
    {
        if (o is XElement)
        {
            var x = ic.ItemContainerGenerator.ContainerFromItem(o);
            if (XNode.DeepEquals((o as XElement), NodeDescription))
            {
                ret = x as TreeViewItem;
            }
            else if ((o as XElement).HasElements){
                if (x != null)
                {
                    bool expanded = (x as TreeViewItem).IsExpanded;
                    (x as TreeViewItem).IsExpanded = true;
                    (x as TreeViewItem).UpdateLayout();
                    ret = SearchNodeInTreeView (x as TreeViewItem, NodeDescription, indent + "  ");
                    if (ret == null)
                    {
                        (x as TreeViewItem).IsExpanded = expanded;
                        (x as TreeViewItem).UpdateLayout();
                    }
                }
            }
        }
        if (ret != null)
        {
            break;
        }
    }
    return ret;
}

我希望我可以帮助任何人。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在SelectedItemChanged事件中更改WPF TreeView SelectedItem

来自分类Dev

WPF将ComboBox ItemSouce绑定到ObservableCollection,并将SelectedItem绑定到DataContext模型属性

来自分类Dev

WPF:绑定到DataGrid中的行的SelectedItem

来自分类Dev

WPF在DataGrid中绑定到DataContext

来自分类Dev

使用WPF在Catel中设置DataContext

来自分类Dev

尽管已设置datacontext,但SelectedItem未知DataContext

来自分类Dev

WPF:如果DataContext直接设置,则只能绑定到List

来自分类Dev

WPF将命令绑定到Datagrid中的Datacontext

来自分类Dev

在构造函数中设置DataContext = this和在WPF中绑定到{RelativeSource Self}之间的区别?

来自分类Dev

在构造函数中设置DataContext = this和在WPF中绑定到{RelativeSource Self}之间的区别?

来自分类Dev

SelectedItem在WPF中的ListView中未绑定

来自分类Dev

在WPF的ListView中,SelectedItem不绑定

来自分类Dev

ItemsSource到TreeView的SelectedItem属性

来自分类Dev

WPF ComboBox SelectedItem绑定到枚举时

来自分类Dev

WPF DataGrid是绑定到CurrentItem还是SelectedItem?

来自分类Dev

WPF RibbonTab绑定到功能区的DataContext

来自分类Dev

在WPF中的XAML中设置DataContext

来自分类Dev

我可以通过绑定到wpf父控件上的属性的依赖项属性在xaml中设置用户控件datacontext吗?

来自分类Dev

在WPF中设置子UserControl的datacontext

来自分类Dev

MVVM中的WPF-TreeView数据绑定

来自分类Dev

如何设置以TreeView样式定义的ContextMenu的DataContext

来自分类Dev

如何设置以TreeView样式定义的ContextMenu的DataContext

来自分类Dev

WPF ComboBox:将SelectedItem设置为不在ItemsSource->绑定奇数中的项目

来自分类Dev

WPF / XAML绑定:使用实际的DataContext

来自分类Dev

已在Window中声明DataContext时绑定DataGrid ---- WPF

来自分类Dev

如何从另一个DataContext绑定ComboBox SelectedItem?

来自分类Dev

绑定到以编程方式设置的DataContext

来自分类Dev

使用Datacontext在WPF C#中的类属性中绑定类

来自分类Dev

使用WPF中的SelectedItem从包含TextBlocks的ListBox中获取值

Related 相关文章

  1. 1

    在SelectedItemChanged事件中更改WPF TreeView SelectedItem

  2. 2

    WPF将ComboBox ItemSouce绑定到ObservableCollection,并将SelectedItem绑定到DataContext模型属性

  3. 3

    WPF:绑定到DataGrid中的行的SelectedItem

  4. 4

    WPF在DataGrid中绑定到DataContext

  5. 5

    使用WPF在Catel中设置DataContext

  6. 6

    尽管已设置datacontext,但SelectedItem未知DataContext

  7. 7

    WPF:如果DataContext直接设置,则只能绑定到List

  8. 8

    WPF将命令绑定到Datagrid中的Datacontext

  9. 9

    在构造函数中设置DataContext = this和在WPF中绑定到{RelativeSource Self}之间的区别?

  10. 10

    在构造函数中设置DataContext = this和在WPF中绑定到{RelativeSource Self}之间的区别?

  11. 11

    SelectedItem在WPF中的ListView中未绑定

  12. 12

    在WPF的ListView中,SelectedItem不绑定

  13. 13

    ItemsSource到TreeView的SelectedItem属性

  14. 14

    WPF ComboBox SelectedItem绑定到枚举时

  15. 15

    WPF DataGrid是绑定到CurrentItem还是SelectedItem?

  16. 16

    WPF RibbonTab绑定到功能区的DataContext

  17. 17

    在WPF中的XAML中设置DataContext

  18. 18

    我可以通过绑定到wpf父控件上的属性的依赖项属性在xaml中设置用户控件datacontext吗?

  19. 19

    在WPF中设置子UserControl的datacontext

  20. 20

    MVVM中的WPF-TreeView数据绑定

  21. 21

    如何设置以TreeView样式定义的ContextMenu的DataContext

  22. 22

    如何设置以TreeView样式定义的ContextMenu的DataContext

  23. 23

    WPF ComboBox:将SelectedItem设置为不在ItemsSource->绑定奇数中的项目

  24. 24

    WPF / XAML绑定:使用实际的DataContext

  25. 25

    已在Window中声明DataContext时绑定DataGrid ---- WPF

  26. 26

    如何从另一个DataContext绑定ComboBox SelectedItem?

  27. 27

    绑定到以编程方式设置的DataContext

  28. 28

    使用Datacontext在WPF C#中的类属性中绑定类

  29. 29

    使用WPF中的SelectedItem从包含TextBlocks的ListBox中获取值

热门标签

归档