Question

I have a problem on binding some controls to my XML.
My application populates a TabControl at runtime, loading the XAML for the TAB with DataTemplateSelector:

class TemplateSelector : DataTemplateSelector
{
  public override DataTemplate SelectTemplate(object item, DependencyObject container)
  {
    if (item != null)
    {
      string templateFile = string.Format("Templates/{0}", 
                                          Properties.Settings.Default.AppId + ".tmpl");
      if (File.Exists(templateFile))
      {
        FileStream fs = new FileStream(templateFile, FileMode.Open);
        DataTemplate template = XamlReader.Load(fs) as DataTemplate;

        Tab tab = item as Tab;
        XmlDataProvider xmlDataProvider = template.Resources["dataProvider"] as XmlDataProvider;
        xmlDataProvider.XPath = tab.BridgeObj.XmlFilePath;

        return template;
      }
    }
    return null;
  }
}

the XAML:

<DataTemplate
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:EurocomCPS;assembly=EurocomCPS">

  <DataTemplate.Resources>
    <local:StringToBoolConverter x:Key="StrToBoolConverter" />
    <local:StringToIntConverter  x:Key="StrToIntConverter" />
    <XmlDataProvider x:Key="dataProvider" XPath="func/parametri/param/BLOCKS"/>
  </DataTemplate.Resources>

  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" Grid.Column="0" Content="ITEM 1:"/>
    <Label Grid.Row="1" Grid.Column="0" Content="ITEM 2:"/>
    <Label Grid.Row="2" Grid.Column="0" Content="ITEM 3:"/>

    <TextBox  Name="TextBox1"
              Grid.Row="0" 
              Grid.Column="1" 
              Text="{Binding XPath='//BLOCK[@id=1]/ITEMS/ITEM[@id=1]/@value'}" />
    <CheckBox Grid.Row="1" 
              Grid.Column="1"
              IsChecked="{Binding XPath='//BLOCK[@id=1]/ITEMS/ITEM[@id=2]/@value',
                            Converter={StaticResource StrToBoolConverter}}"/>

    <CheckBox Grid.Row="2" 
              Grid.Column="1"
              IsChecked="{Binding XPath='//BLOCK[@id=1]/ITEMS/ITEM[@id=3]/@value',
                            Converter={StaticResource StrToBoolConverter}}"/>

  </Grid>
</DataTemplate>

Every page hold an XmlDataProvider which load the following xml file:

<func id="A29086">
  <parametri>
    <param>
      <BLOCKS max_count="2" write_id="49" read_req_id="47" read_rep_id="48" session_id="7">
        <BLOCK id="1" frame="1" framelen="61">
          <ITEMS max_count="14">
            <ITEM id="1" type="CHAR" size="1" value="0" />
            <ITEM id="2" type="CHAR" size="1" value="1" />
            <ITEM id="3" type="CHAR" size="1" value="0" />
            ...
          </ITEMS>
        </BLOCK>
        <BLOCK id="2" frame="1" framelen="61">
          <ITEMS max_count="14">
            <ITEM id="1" type="CHAR" size="1" value="0" />
            <ITEM id="2" type="CHAR" size="1" value="1" />
            ...
          </ITEMS>
        </BLOCK>
      </BLOCKS>
    </param>
  </parametri>
</func>

When running I get this error:

System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//BLOCK[@id=1]/ITEMS/ITEM[@id=1]/@value' BindingExpression:Path=/InnerText; DataItem='Tab' (HashCode=57706919); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') Tab:'EurocomCPS.Tab'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//BLOCK[@id=1]/ITEMS/ITEM[@id=2]/@value' BindingExpression:Path=/InnerText; DataItem='Tab' (HashCode=57706919); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1') Tab:'EurocomCPS.Tab'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//BLOCK[@id=1]/ITEMS/ITEM[@id=3]/@value' BindingExpression:Path=/InnerText; DataItem='Tab' (HashCode=57706919); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1') Tab:'EurocomCPS.Tab'

--- EDIT ---

I add the DataContext to my controls but I still have problems.
The first is that I get the following:

System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//BLOCK[@id=1]/ITEMS/ITEM[@id=1]/@value' BindingExpression:Path=/InnerText; DataItem='Tab' (HashCode=46144604); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') Tab:'EurocomCPS.Tab'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//BLOCK[@id=1]/ITEMS/ITEM[@id=1]/@value' BindingExpression:Path=/InnerText; DataItem='Tab' (HashCode=46144604); target element is 'TextBox' (Name='TextBox1'); target property is 'Text' (type 'String') Tab:'EurocomCPS.Tab'

And I don't understand what is the first 'unnamed' textbox which is not defined anywhere.

Second is that, if I put a converter to the TextBox binding (eg, like the one I use for the CheckBoxes) I don't get the error.

Third the Converter functions are not called.

Was it helpful?

Solution

Assuming that you somehow load XML into XmlDataProvider, as I don't see Source defined anywhere, your XPath is fine but you don't provide the Source for your binding. It should work if you change it to something like this:

<TextBlock Text="{Binding Source={StaticResource dataProvider}, XPath='//BLOCK[@id=1]/ITEMS/ITEM[@id=2]/@value'}"/>

if you won't specify source by default it will look in DataContext.

OTHER TIPS

I think that '@id=1' is not the correct way of doing it as per blog.

The problem was the method using to assign the XML file!!

xmlDataProvider.XPath = tab.BridgeObj.XmlFilePath;

Changing it to

xmlDataProvider.Source = new Uri(tab.BridgeObj.XmlFilePath);

has solved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top