需要将WPF RadGridView绑定到XML文档并在网格中显示属性值

发布于 2024-10-14 13:28:41 字数 1330 浏览 3 评论 0原文

我正在开发一个 WPF 应用程序,需要将 RadGridView 绑定到 XML 文档,并且对于我的列,显示来自节点属性的数据。

目前,它为我提供了正确的行数,但它显示的不是我正在查找的值,而是字符串“System.Xml.XmlElement”,

这是我的 XAML。

<telerik:RadGridView Name="uxSettings" ItemsSource="{Binding XmlSettings}" AutoGenerateColumns="False"
RowIndicatorVisibility="Visible"  Grid.Row="2" Grid.Column="1" Height="150" 
ShowGroupPanel="False" Width="350"  Margin="10"  IsFilteringAllowed="False">
<telerik:RadGridView.Columns>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding XPath=@ID}" Header="Item Name" Width="300"/>
</telerik:RadGridView.Columns>

这是我要绑定的 xml。

<settings>
<setting ID="OutputXOffset" value="12" />
<setting ID="OutputYOffset" value="12" />
</settings>

我实际上绑定到像这样填充的 ObservableCollection 集合。

public ObservableCollection<XmlNode> PopulateXmlSettings(XmlDocument settingsDoc)

{ XmlDataProvider 提供者 = new XmlDataProvider(); 提供者.IsAsynchronous = false; 提供者.Document = settingsDoc; provider.XPath = "设置/设置";

return  new ObservableCollection<XmlNode>((IEnumerable<XmlNode>)provider.Data);

理想情况下,

我希望 ID 的“OutputXOffset 和 OutputYOffset”可见,并且只有值“12”可编辑。

I'm working on a a WPF application and need to bind a RadGridView to an XML document and, for my columns, display data from the attributes of the node.

Currently it's giving me to correct number of rows but instead of the values I'm looking for it displays the string "System.Xml.XmlElement"

Here's my XAML.

<telerik:RadGridView Name="uxSettings" ItemsSource="{Binding XmlSettings}" AutoGenerateColumns="False"
RowIndicatorVisibility="Visible"  Grid.Row="2" Grid.Column="1" Height="150" 
ShowGroupPanel="False" Width="350"  Margin="10"  IsFilteringAllowed="False">
<telerik:RadGridView.Columns>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding XPath=@ID}" Header="Item Name" Width="300"/>
</telerik:RadGridView.Columns>

And here's the xml I'm binding to.

<settings>
<setting ID="OutputXOffset" value="12" />
<setting ID="OutputYOffset" value="12" />
</settings>

I'm actually binding to an ObservableCollection collection that I'm populating like this.

public ObservableCollection<XmlNode> PopulateXmlSettings(XmlDocument settingsDoc)

{
XmlDataProvider provider = new XmlDataProvider();
provider.IsAsynchronous = false;
provider.Document = settingsDoc;
provider.XPath = "settings/setting";

return  new ObservableCollection<XmlNode>((IEnumerable<XmlNode>)provider.Data);

}

Ideally I want the ID's "OutputXOffset and OutputYOffset" to be viewable and only the values "12" to be editable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独陪着我 2024-10-21 13:28:41

不要直接绑定到 XmlNode,而是创建您自己的类型,公开 UI 所需的适当属性并绑定到这些属性的集合。

另一个选择是从 XmlNode 继承并重写 ToString() 以返回要在列中显示的值...但我不建议这样做。我更喜欢创建 POCO 并使用数据源中的数据填充它们并绑定到 POCO。

从头开始​​;看来你无论如何都不能从 XmlNode 继承。

Instead of binding directly to XmlNodes, create your own type that exposes the appropriate properties needed for your UI and bind to a collection of those.

Another option is to inherit from XmlNode and override ToString() to return the value you want to display in your column... but I don't recommend this. I prefer creating POCOs and populating them with data from the datasource and binding to the POCOs.

Scratch that; it seems you can't inherit from XmlNode anyway.

ぽ尐不点ル 2024-10-21 13:28:41

您可以尝试首先在窗口定义中包含解决方案命名空间,然后在资源中包含 observablecollection 集合类,如下所示

<Window.Resources>
     <solutionNameSpace:ClassName x:Key="Class1" />
</Window.Resources>
.
.

然后更新控件,如下所示

<telerik:RadGridView ....... ItemSource="{StaticResource Class1}" ....>
<telerik:RadGridView.Columns>
    <telerik:GridViewColumn DataMemberBinding="{Binding Path=ID}" .....>
</telerik.RadView.Columns>

You can try by first including your solution namespace in the window definition and then including the observablecollection collection class in resources like following

<Window.Resources>
     <solutionNameSpace:ClassName x:Key="Class1" />
</Window.Resources>
.
.

Then updating control like following

<telerik:RadGridView ....... ItemSource="{StaticResource Class1}" ....>
<telerik:RadGridView.Columns>
    <telerik:GridViewColumn DataMemberBinding="{Binding Path=ID}" .....>
</telerik.RadView.Columns>
葬﹪忆之殇 2024-10-21 13:28:41

看起来像是您可能想要报告的 Telerik 控件中的错误。为了测试这一点,我写了你的 xml;

<XmlDataProvider x:Key="XmlSettings" XPath="/settings/setting">
  <x:XData>
    <settings xmlns="">
      <setting ID="OutputXOffset" value="12"/>
      <setting ID="OutputYOffset" value="12"/>
    </settings>
  </x:XData>
</XmlDataProvider>

然后使用 DataGrid 和 telerik RadGridView 绑定到它,例如;

<DataGrid Grid.Row="0" AutoGenerateColumns="False"
    ItemsSource="{Binding Source={StaticResource XmlSettings}}">
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding XPath=@ID}"/>
    <DataGridTextColumn Binding="{Binding XPath=@value}"/>
  </DataGrid.Columns>
</DataGrid>

<telerik:RadGridView Grid.Row="1" AutoGenerateColumns="False"
    ItemsSource="{Binding Source={StaticResource XmlSettings}}">
  <telerik:RadGridView.Columns>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding XPath=@ID}"/>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding XPath=@value}"/>
  </telerik:RadGridView.Columns>
</telerik:RadGridView>

DataGrid 可以工作,但 RadGridView 不能,而是返回一个 xmlElement,如您所说。为了获取 xmlElement 的值,我想为绑定编写一个简单的转换器 (IValueConverter)。一旦我这样做,我发现转换器接收整个“设置”元素而不是 XPath 指向的属性,因此转换器看起来像;

Imports System.Xml

<ValueConversion(GetType(XmlElement), GetType(String))> _
Public Class xmlElement2String
    Implements IValueConverter

  Public Function Convert(value As Object, targetType As Type, 
      parameter As Object, culture As System.Globalization.CultureInfo
      ) As Object Implements System.Windows.Data.IValueConverter.Convert
    Dim anXmlElement As XmlElement = TryCast(value, XmlElement)
    If anXmlElement is Nothing then Return Binding.DoNothing
    Return anXmlElement.SelectSingleNode(CType(parameter,String)).Value
  End Function

  Public Function ConvertBack(value As Object, targetType As Type, 
      parameter As Object, culture As System.Globalization.CultureInfo
      ) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
    throw new System.NotImplementedException("Not implemented.")
  End Function
End Class

因此,您可以在 Xaml 中向值转换器添加引用,例如;

<local:xmlElement2String x:Key="xmlElement2String"/>

并将您的绑定更改为;

<telerik:GridViewDataColumn DataMemberBinding="{Binding XPath=@ID, 
    Converter={StaticResource xmlElement2String}, ConverterParameter=@ID}"/>
<telerik:GridViewDataColumn DataMemberBinding="{Binding
    Converter={StaticResource xmlElement2String}, ConverterParameter=@value}"/>

请注意,对于“ID”,我保留了 XPath 语句,对于“值”,我保留了它,这没有什么区别。希望这有帮助...

Looks like a bug in the Telerik control you might want to report. To test this I wrote your xml like;

<XmlDataProvider x:Key="XmlSettings" XPath="/settings/setting">
  <x:XData>
    <settings xmlns="">
      <setting ID="OutputXOffset" value="12"/>
      <setting ID="OutputYOffset" value="12"/>
    </settings>
  </x:XData>
</XmlDataProvider>

and then bound to it with both a DataGrid and a telerik RadGridView like;

<DataGrid Grid.Row="0" AutoGenerateColumns="False"
    ItemsSource="{Binding Source={StaticResource XmlSettings}}">
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding XPath=@ID}"/>
    <DataGridTextColumn Binding="{Binding XPath=@value}"/>
  </DataGrid.Columns>
</DataGrid>

<telerik:RadGridView Grid.Row="1" AutoGenerateColumns="False"
    ItemsSource="{Binding Source={StaticResource XmlSettings}}">
  <telerik:RadGridView.Columns>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding XPath=@ID}"/>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding XPath=@value}"/>
  </telerik:RadGridView.Columns>
</telerik:RadGridView>

and the DataGrid works but the RadGridView doesn't and instead returns an xmlElement as you said. To get at the xmlElement's value I figured I'd write a simple Converter (IValueConverter) for the binding. Once I did this I found that the converter receives the entire "setting" element instead of the attribute pointed to by the XPath so the converter would look like;

Imports System.Xml

<ValueConversion(GetType(XmlElement), GetType(String))> _
Public Class xmlElement2String
    Implements IValueConverter

  Public Function Convert(value As Object, targetType As Type, 
      parameter As Object, culture As System.Globalization.CultureInfo
      ) As Object Implements System.Windows.Data.IValueConverter.Convert
    Dim anXmlElement As XmlElement = TryCast(value, XmlElement)
    If anXmlElement is Nothing then Return Binding.DoNothing
    Return anXmlElement.SelectSingleNode(CType(parameter,String)).Value
  End Function

  Public Function ConvertBack(value As Object, targetType As Type, 
      parameter As Object, culture As System.Globalization.CultureInfo
      ) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
    throw new System.NotImplementedException("Not implemented.")
  End Function
End Class

so you would add a reference in your Xaml to the value converter like;

<local:xmlElement2String x:Key="xmlElement2String"/>

and change your bindings to;

<telerik:GridViewDataColumn DataMemberBinding="{Binding XPath=@ID, 
    Converter={StaticResource xmlElement2String}, ConverterParameter=@ID}"/>
<telerik:GridViewDataColumn DataMemberBinding="{Binding
    Converter={StaticResource xmlElement2String}, ConverterParameter=@value}"/>

notice that for "ID" I left the XPath statement in and for "value" I left it off and it makes no difference. Hope this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文