How to bind TextBoxes to an XML File (with namespace)
I use C#. I want to make a Twoways bind between Textboxes and an XML data source. In order to achieve that, I have written this:
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);
Where:
XPath = "dummyns/@totalConcept"
XmlManager.Instance.CreateAttributeOrElement
creates the attribute in the XML Document which the bind will to do to TextBoxes.- The
CreateAttributeOrElement
method of theXMLManager
object returns some like this:totalConcept=""
There is a commented line which creates the attribute. The o开发者_如何学JAVAther way is ot make it implicit in the instantiation line of PropertyPath
. When either of the ways is executed, it generates an XML Document like this:
<cna:dummyns xmlns:cna=\"http://tempuri.org/XMLSchema.xsd\" totalConcept=\"\" />
But when I assign a value to Textbox
, I get this in the Output window:
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 ''
So the Bind is not working... In the line #6, I also tried:
string.Format("XmlManager.Instancia.declaracion.Root.Attribute[\"{0}\"].Value", XPath)
But I got the same result.
Has anybody got something similar to this working?
Any comments or suggestions are welcome.
Have you tried something like this ?
<XmlDataProvider x:Key="dataProvider" XPath="RootElement" Source="XMLFile1.xml"/>
...
<TextBox Text="{Binding Source={StaticResource dataProvider}, XPath=//ChildElement/@totalConcept }" />
精彩评论