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

时间:2009-06-19 12:57:41

标签: c# data-binding xaml xmldataprovider

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

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

SomeClass.xaml - Works

<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

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的{SOME MAGIC}属性中的ItemsSource

<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

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

1 个答案:

答案 0 :(得分:1)

我发现一个选项是设置控件的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;
    }
}