XmlDataProvider上的绑定错误(无法绑定到非XML对象)

时间:2013-08-26 07:47:59

标签: c# wpf xml xmldataprovider

我在将一些控件绑定到XML时遇到问题 我的应用程序在运行时填充TabControl,使用DataTemplateSelector为TAB加载XAML:

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;
  }
}

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>

每个页面都有一个XmlDataProvider,它加载以下xml文件:

<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>

运行时我收到此错误:

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'

---编辑---

我将DataContext添加到我的控件中,但我仍然有问题 第一个是我得到以下内容:

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'

我不明白第一个“未命名”的文本框是什么,它没有在任何地方定义。

第二是,如果我将一个转换器放到TextBox绑定中(例如,就像我用于CheckBoxes的那个),我没有得到错误。

第三不调用转换器功能。

3 个答案:

答案 0 :(得分:0)

我认为'@ id = 1'不是按照blog执行此操作的正确方法。

答案 1 :(得分:0)

假设您以某种方式将XML加载到XmlDataProvider,因为我没有在任何地方看到Source定义,您的XPath没问题,但您没有提供Source为你的约束力。如果你把它改成这样的话应该可以工作:

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

如果您默认情况下不指定来源,则会查看DataContext

答案 2 :(得分:0)

问题是用于分配XML文件的方法!!

xmlDataProvider.XPath = tab.BridgeObj.XmlFilePath;

将其更改为

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

已经解决了。