如何将XmlDataProvider类属性绑定到XAML

如何将XmlDataProvider类属性绑定到XAML

本文介绍了如何将XmlDataProvider类属性绑定到XAML TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个在XAML中定义的TreeView控件绑定到其代码隐藏类中的一个属性。我已经阅读了,但当我尝试使用XmlDataProvider作为绑定源时,页面底部的注释中的示例不起作用。

I would like to bind a TreeView control I have defined in XAML to a property in its code-behind class. I already read through a WPF Basic Data Binding FAQ, but the example in the comments at the very bottom of the page didn't work when I tried to use an XmlDataProvider as the binding source.

如何修改以下代码,以便绑定在XAML中定义,而不是在类的构造函数中定义?换句话说,如何修改TreeView的 ItemsSource 属性来引用其代码隐藏类中的属性?

How can I modify the following code so that the binding is defined in the XAML, rather than in the class's constructor? In other words, how can I modify the TreeView's ItemsSource attribute to reference a property in its code-behind class?

<UserControl x:Class="SomeNamespace.SomeClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UserControl.Resources>
        <XmlDataProvider x:Key="SomeTreeData" />
    </UserControl.Resources>
    <TreeView Name="SomeTree" ItemsSource="{Binding Source={StaticResource SomeTreeData}, XPath=*}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="items" ItemsSource="{Binding XPath=*}">
                <TextBlock Text="{Binding XPath=@Header}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="item" ItemsSource="{Binding XPath=*}">
                <TextBlock Text="{Binding XPath=@Header}" />
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</UserControl>



SomeClass.xaml.cs - Works



SomeClass.xaml.cs - Works

public partial class SomeClass : UserControl
{
    public SomeClass()
    {
        InitializeComponent();

        XmlDataProvider lSomeTreeData
            = this.FindResource("SomeTreeData") as XmlDataProvider;
        lSomeTreeData.Document = new XmlDocument();
        lSomeTreeData.Document.LoadXml("<items xmlns=\"\" Header=\"Some items\"><item Header=\"Some item\" /></items>");
    }
}



SomeClass.xaml - Desired



请注意TreeView的 ItemsSource 属性中的 {SOME MAGIC}

<UserControl x:Class="SomeNamespace.SomeClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TreeView Name="SomeTree" ItemsSource="{Binding Source={SOME MAGIC}, XPath=*}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="items" ItemsSource="{Binding XPath=*}">
                <TextBlock Text="{Binding XPath=@Header}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="item" ItemsSource="{Binding XPath=*}">
                <TextBlock Text="{Binding XPath=@Header}" />
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</UserControl>



SomeClass.xaml.cs - Desired



SomeClass.xaml.cs - Desired

public partial class SomeClass : UserControl
{
    public XmlDataProvider SomeXmlDataProvider { get; set; }

    public SomeClass()
    {
        InitializeComponent();

        this.SomeXmlDataProvider = new XmlDataProvider();
        this.SomeXmlDataProvider.Document = new XmlDocument();
        this.SomeXmlDataProvider.Document.LoadXml("<items xmlns=\"\" Header=\"Some items\"><item Header=\"Some item\" /></items>");
    }
}


推荐答案

我发现一个选项是设置控件的 DataContext

I've discovered that one option is to set the control's DataContext:

<UserControl x:Class="SomeNamespace.SomeClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TreeView ItemsSource="{Binding XPath=/items}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="items" ItemsSource="{Binding XPath=*}">
                <TextBlock Text="{Binding XPath=@Header}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="item" ItemsSource="{Binding XPath=*}">
                <TextBlock Text="{Binding XPath=@Header}" />
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</UserControl>







public partial class SomeClass : UserControl
{
    public XmlDataProvider SomeXmlDataProvider { get; set; }

    public SomeClass()
    {
        InitializeComponent();

        this.SomeXmlDataProvider = new XmlDataProvider();
        this.SomeXmlDataProvider.Document = new XmlDocument();
        this.SomeXmlDataProvider.Document.LoadXml("<items Header=\"Some items\"><item Header=\"Some item\" /></items>");

        this.DataContext = this.SomeXmlDataProvider.Document;
    }
}

这篇关于如何将XmlDataProvider类属性绑定到XAML TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:07