自定义XML数据提供程序(WPF)

时间:2011-03-17 20:54:53

标签: wpf data-binding

我想创建自己的XML数据提供程序派生自DataSourceProvider(类似的XmlDataProvider)。

(我不想使用XmlDataProvider,因为如果XPath查询失败,我想要返回备用数据)

但我无法理解如何访问通过Binding.XPath设置的XPath属性。

例如,我有课程:

public class MyXmlDataProvider : DataSourceProvider
{
    private string _xPath;
    public string XPath
    {
        // The following code i spied from XmlDataProvider implementation using .Net Reflector
        get
        {
            return this._xPath;
        }
        set    // WHY binding do not call this setter? ((
        {
            if (this._xPath != value)
            {
                this._xPath = value;
                if (!base.IsRefreshDeferred)
                {
                    base.Refresh();
                }
            }
        }
    }

    private string _result;
    protected override void BeginQuery()
    {
        // .... getting result using XPath

        base.OnQueryFinished(_result);
    }
}

XAML绑定示例(mydata - MyXmlDataProvider类的实例):

<TextBlock Text="{Binding Source={StaticResource mydata}, XPath=/main/version}" />

问题是:如何在MyXmlDataProvider类中获取Binding.XPath值?

1 个答案:

答案 0 :(得分:2)

我在这里找到了XmlDataProvider的源代码:

http://reflector.webtropy.com/default.aspx/DotNET/DotNET/8@0/untmp/WIN_WINDOWS/lh_tools_devdiv_wpf/Windows/wcp/Framework/System/Windows/Data/XmlDataProvider@cs/4/XmlDataProvider@cs

但是我通过实现一个带索引器的类(不是DataSourceProvider的后代)解决了我的任务:

public string this[string xpath]
{
  // Here i do XPath-query and handle its result
  ...
}

XPath-query我在绑定中设置如下:

<TextBlock Text="{Binding Source={StaticResource mydata}, Path=[/main/version]}" />