【发布时间】: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