2011-03-17 60 views
0

我想創建自己的DataSourceProvider(類似XmlDataProvider)派生的XML數據提供程序。自定義XML數據提供程序(WPF)

(我不想使用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

我發現這裏XmlDataProvider的源代碼:

http://reflector.webtropy.com/default.aspx/DotNET/DotNET/[email protected]/untmp/WIN_WINDOWS/lh_tools_devdiv_wpf/Windows/wcp/Framework/System/Windows/Data/[email protected]/4/[email protected]

但我通過實現與索引類(不DataSourceProvider的後代),解決了我的任務:

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

的XPath查詢我在綁定中設置如下:

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