2011-05-17 13 views
5

我使用C#。 我想在Textboxes和XML數據源之間創建一個Twoways綁定。爲了實現這一目標,我寫了這個:如何將文本框綁定到XML文件(帶名稱空間)

1 Binding source = new Binding(); 
2 
3 // object ddd = XmlManager.Instance.CreateAttributeOrElement(XPath); 
4 source.Path = 
5  new PropertyPath(
6   string.Format("(XmlManager.Instance.CreateAttributeOrElement({0}))", XPath)); 
7 source.Mode = BindingMode.TwoWay; 
8 
9 UIElementos.UiTexto textoCampo = this as UIElementos.UiTexto; 
10 textoCampo.elementoTexto.SetBinding(TextBox.TextProperty, source); 

其中:

  • XPath = "dummyns/@totalConcept"
  • XmlManager.Instance.CreateAttributeOrElement創建的XML文檔綁定會做文本框該屬性。
  • XMLManager對象的CreateAttributeOrElement方法返回一些這樣的: totalConcept=""

有一個註釋行它創建的屬性。另一種方式是使其隱含在PropertyPath的實例化行中。當執行任意的方式,它會生成一個XML文件是這樣的:

<cna:dummyns xmlns:cna=\"http://tempuri.org/XMLSchema.xsd\" totalConcept=\"\" /> 

但是,當我把值賦給Textbox,我得到這個在輸出窗口:

System.Windows.Data Warning: 78 : BindingExpression (hash=3146959): TransferValue - got raw value {DependencyProperty.UnsetValue} 
System.Windows.Data Warning: 86 : BindingExpression (hash=3146959): TransferValue - using fallback/default value '' 
System.Windows.Data Warning: 87 : BindingExpression (hash=3146959): TransferValue - using final value '' 

所以綁定不工作... 在6號線,我也試過:

string.Format("XmlManager.Instancia.declaracion.Root.Attribute[\"{0}\"].Value", XPath) 

,但我得到了同樣的結果。

有沒有人有類似的工作?

歡迎任何意見或建議。

+0

您是否嘗試過使用XPath屬性而不是Path屬性?從[MSDN](http://msdn.microsoft.com/zh-cn/library/ms742451.aspx):「用於綁定到XML數據源的真正的XPath表達式不用作Path值,而應該用於爲互斥XPath屬性「 – 2011-05-22 13:34:27

回答

0

你試過類似的東西嗎?

<XmlDataProvider x:Key="dataProvider" XPath="RootElement" Source="XMLFile1.xml"/> 
... 

<TextBox Text="{Binding Source={StaticResource dataProvider}, XPath=//ChildElement/@totalConcept }" /> 
相關問題