WPF TreeView 刷新 [英] WPF TreeView refreshing

查看:41
本文介绍了WPF TreeView 刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题.我在 WPF 项目中使用 TreeView 来可视化我的 XML 数据.问题是,当我编辑 XmlDocument 时,它不会在 TreeView 中刷新.但是我注意到当我检查 SelectedNode 时,它是我编辑的和 XmlNode.所以我的编辑"方法工作正常,但我的树的视觉刷新只有一个问题..Refresh().Items.Refresh() 也不起作用.

I've got a problem. I use TreeView in my WPF project to visualize my XML data. The problem is, when I edit my XmlDocument it doesn't refresh in TreeView. But I noticed that when I check SelectedNode, it is my editted and XmlNode. So my "Edit" method works fine, but there's only a problem in visual refresh of my tree. .Refresh() or .Items.Refresh() don't work either.

这是我的树的模板:

<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="?" FontFamily="Consolas" FontSize="8pt" />
        <ItemsControl
            ItemTemplate="{StaticResource AttributeTemplate}"
            ItemsSource="{Binding Path=Attributes}"
            HorizontalAlignment="Center">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </StackPanel>
    <HierarchicalDataTemplate.ItemsSource>
        <Binding XPath="*" />
    </HierarchicalDataTemplate.ItemsSource>
    <HierarchicalDataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
            <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Value, Mode=TwoWay}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
            <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}"/>
        </DataTrigger>
    </HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>

<Style x:Key="TreeViewAllExpandedStyle"  TargetType="{x:Type TreeView}">
    <Style.Resources>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="True" />
        </Style>
    </Style.Resources>
</Style>

<Style x:Key="TreeViewAllCollapsedStyle" TargetType="{x:Type TreeView}">
    <Style.Resources>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="False" />
        </Style>
    </Style.Resources>
</Style>

这里是Window.Resources:

<Window.Resources>
    <XmlDataProvider x:Key="XmlData" />
</Window.Resources>

这是我的树:

<TreeView x:Name="XmlTree" Grid.Row="1"
      ItemsSource="{Binding Source={StaticResource XmlData}, XPath=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
      ItemTemplate="{StaticResource NodeTemplate}"
      SelectedItemChanged="XmlTree_SelectedItemChanged" />

这是我的代码:

private XmlDocument _xml;
private XmlElement _selectedElement;
private XmlDataProvider _xmlDataProvider;

private void MainWindow_Load(object sender, EventArgs e)
{
    XmlTree.Style = (Style)FindResource("TreeViewAllExpandedStyle");
    _xmlDataProvider = FindResource("XmlData") as XmlDataProvider;
}

private void OpenXmlFile(string filePath)
{
    _xml = new XmlDocument();
    _xml.Load(filePath);

    _xmlDataProvider.Document = _xml;
}

private void SaveChangesButton_Click(object sender, EventArgs e)
{
    Dictionary<string, string> newAttributes = GetChangedAttributes();
    foreach (KeyValuePair<string, string> pair in newAttributes)
    {
        _selectedElement.SetAttribute(pair.Key, pair.Value);
    }

    RefreshViews();
}

private void RefreshViews()
{
    // now I don't know what to do here, any Refresh doesn't work:S
}

第二件事是,如何清除我的树以便能够再次将它用于其他数据(我在尝试 XmlTree.Items.Clear() 时遇到了 NullReferenceException;

The second thing is, how to clear my tree in order to be able to use it again for another data (I've got NullReferenceException while trying XmlTree.Items.Clear();

推荐答案

折腾了好几个小时终于找到了解决方案!

After many hours finally found a solution!

private void RefreshViews()
{
    XmlEditor.Clear();
    XmlEditor.Text = IndentXml();

    UnselectSelectedItem();

    XmlTree.Items.Refresh();
    XmlTree.UpdateLayout();
}

private void UnselectSelectedItem()
{
    if (XmlTree.SelectedItem != null)
    {
        var container = FindTreeViewSelectedItemContainer(XmlTree, XmlTree.SelectedItem);
        if (container != null)
        {
            container.IsSelected = false;
        }
    }
}

private static TreeViewItem FindTreeViewSelectedItemContainer(ItemsControl root, object selection)
{
    var item = root.ItemContainerGenerator.ContainerFromItem(selection) as TreeViewItem;
    if (item == null)
    {
        foreach (var subItem in root.Items)
        {
            item = FindTreeViewSelectedItemContainer((TreeViewItem)root.ItemContainerGenerator.ContainerFromItem(subItem), selection);
            if (item != null)
            {
                break;
            }
        }
    }

    return item;
}

这篇关于WPF TreeView 刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆