简体   繁体   English

如何将XmlDataProvider类属性绑定到XAML TreeView

[英]How to Bind an XmlDataProvider Class Property to a XAML TreeView

I would like to bind a TreeView control I have defined in XAML to a property in its code-behind class. 我想将XAML中定义的TreeView控件绑定到其代码隐藏类中的属性。 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. 我已经阅读了WPF基本数据绑定常见问题解答 ,但是当我尝试使用XmlDataProvider作为绑定源时,页面底部注释中的示例不起作用。

How can I modify the following code so that the binding is defined in the XAML, rather than in the class's constructor? 如何修改以下代码,以便在XAML中而不是在类的构造函数中定义绑定? In other words, how can I modify the TreeView's ItemsSource attribute to reference a property in its code-behind class? 换句话说,如何修改TreeView的ItemsSource属性以引用其代码隐藏类中的属性?

SomeClass.xaml - Works SomeClass.xaml-作品

<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-作品

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 SomeClass.xaml-所需

Note the {SOME MAGIC} in the TreeView's ItemsSource attribute. 注意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-所需

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>");
    }
}

I've discovered that one option is to set the control's DataContext : 我发现一种选择是设置控件的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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM