如何将TextBoxes绑定到XML文件(带有命名空间)

Fer*_*erR 5 c# xml data-binding wpf xaml

我用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);
Run Code Online (Sandbox Code Playgroud)

哪里:

  • XPath = "dummyns/@totalConcept"
  • XmlManager.Instance.CreateAttributeOrElement 在XML文档中创建属性,绑定将对TextBox进行该属性.
  • 对象的CreateAttributeOrElement方法XMLManager返回如下: totalConcept=""

有一条注释行可以创建属性.另一种方法是将其隐含在实例化行中PropertyPath.当执行任何一种方法时,它会生成一个XML文档,如下所示:

<cna:dummyns xmlns:cna=\"http://tempuri.org/XMLSchema.xsd\" totalConcept=\"\" />
Run Code Online (Sandbox Code Playgroud)

但是当我分配一个值时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 ''
Run Code Online (Sandbox Code Playgroud)

所以Bind没有工作......在第6行,我也尝试过:

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

但我得到了同样的结果.

有人有类似的工作吗?

欢迎提出任何意见或建议.

m1k*_*1k4 0

你尝试过这样的事情吗?

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

<TextBox Text="{Binding Source={StaticResource dataProvider}, XPath=//ChildElement/@totalConcept }" />
Run Code Online (Sandbox Code Playgroud)