【问题标题】:How to bind TextBoxes to an XML File (with namespace)如何将文本框绑定到 XML 文件(带有命名空间)
【发布时间】:2011-08-27 10:50:04
【问题描述】:

我使用 C#。 我想在文本框和 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 文档中创建属性,该属性将绑定到 TextBoxes。
  • 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)

但我得到了相同的结果。

有人有类似的工作吗?

欢迎任何cmets或建议。

【问题讨论】:

  • 您是否尝试过使用 XPath 属性而不是 Path 属性?来自MSDN:“用于绑定到 XML 数据源的真正 XPath 表达式不用作 Path 值,而应用于互斥 XPath 属性”

标签: c# wpf xml xaml data-binding


【解决方案1】:

你试过这样的吗?

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多