简体   繁体   English

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

[英]Binding error on XmlDataProvider (cannot bind to non-XML object)

I have a problem on binding some controls to my XML. 我在将某些控件绑定到XML时遇到问题。
My application populates a TabControl at runtime, loading the XAML for the TAB with DataTemplateSelector: 我的应用程序在运行时填充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;
  }
}

the XAML: 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: 每个页面都有一个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>

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. 我将DataContext添加到控件中,但是仍然有问题。
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. 其次是,如果我将转换器与TextBox绑定(例如,与我用于CheckBox的转换器一样),则不会收到该错误。

Third the Converter functions are not called. 第三 ,不调用Converter函数。

我认为'@ id = 1'不是按照Blog的正确方法。

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. 假设您以某种方式将XML加载到XmlDataProvider ,因为我在任何地方都没有看到Source定义,那么您的XPath很好,但是您没有为绑定提供Source 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 . 如果默认情况下不指定源,它将在DataContext查找。

The problem was the method using to assign the XML file!! 问题是用于分配XML文件的方法!!

xmlDataProvider.XPath = tab.BridgeObj.XmlFilePath;

Changing it to 更改为

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

has solved. 解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试与我的控件绑定时遇到“ XPath无法绑定到非XML对象”的问题,但可与TextBox一起使用 - Got “XPath can't bind to non-XML object” when try to bind with my control, but works with TextBox 如何解析其中包含非XML数据的xml - How to parse an xml that has non-xml data in it 在C#中解析NON-XML语法的TEXT文件 - Parsing NON-XML syntax'ed TEXT file in C# c#简单的非xml配置文件? - c# simple non-xml config files? 如何在视图中将ViewModel的XmlDataProvider属性与XmlDataProvider绑定? - How to bind XmlDataProvider property of viewmodel with XmlDataProvider in view? 在背后的代码中绑定到XMLDataProvider - Binding to XMLDataProvider in Code Behind 使用MVVM模式绑定xmldataprovider - Bind xmldataprovider using MVVM pattern 通过XmlDataProvider将复选框“ IsChecked”值绑定到外部XML文件(利用MVVM) - Binding Checkbox “IsChecked” value to external XML file via XmlDataProvider (utilizing MVVM) 在非xml环境中对linq进行解耦的通用键值访问 - decoupled generic key-value access against linq in a non-xml environment C#LINQ-从单个元素检索多个属性,转换为结构化(非XML)文本 - C# LINQ - Retrieve multiple attributes from single element, convert to structured (non-xml) text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM