小编Fer*_*erR的帖子

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

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

c# xml data-binding wpf xaml

5
推荐指数
1
解决办法
1312
查看次数

标签 统计

c# ×1

data-binding ×1

wpf ×1

xaml ×1

xml ×1