簡體   English   中英

如何將TextBoxes綁定到XML文件(帶有命名空間)

[英]How to bind TextBoxes to an XML File (with namespace)

我用C#。 我想在Textboxes和XML數據源之間進行Twoways綁定。 為了達到這個目的,我寫了這個:

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文檔中創建綁定將對TextBoxes執行的屬性。
  • XMLManager對象的CreateAttributeOrElement方法返回如下: totalConcept=""

有一條注釋行可以創建屬性。 另一種方法是將它隱含在PropertyPath的實例化行中。 當執行任何一種方法時,它會生成一個XML文檔,如下所示:

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

但是當我為Textbox分配一個值時,我在Output窗口中得到了這個值:

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 ''

所以Bind沒有工作......在第6行,我也嘗試過:

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

但我得到了同樣的結果。

有人有類似的工作嗎?

歡迎提出任何意見或建議。

你嘗試過這樣的事嗎?

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM