需要将WPF RadGridView绑定到XML文档并在网格中显示属性值
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要直接绑定到 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.
您可以尝试首先在窗口定义中包含解决方案命名空间,然后在资源中包含 observablecollection 集合类,如下所示
然后更新控件,如下所示
You can try by first including your solution namespace in the window definition and then including the observablecollection collection class in resources like following
Then updating control like following
看起来像是您可能想要报告的 Telerik 控件中的错误。为了测试这一点,我写了你的 xml;
然后使用 DataGrid 和 telerik RadGridView 绑定到它,例如;
DataGrid 可以工作,但 RadGridView 不能,而是返回一个 xmlElement,如您所说。为了获取 xmlElement 的值,我想为绑定编写一个简单的转换器 (IValueConverter)。一旦我这样做,我发现转换器接收整个“设置”元素而不是 XPath 指向的属性,因此转换器看起来像;
因此,您可以在 Xaml 中向值转换器添加引用,例如;
并将您的绑定更改为;
请注意,对于“ID”,我保留了 XPath 语句,对于“值”,我保留了它,这没有什么区别。希望这有帮助...
Looks like a bug in the Telerik control you might want to report. To test this I wrote your xml like;
and then bound to it with both a DataGrid and a telerik RadGridView like;
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;
so you would add a reference in your Xaml to the value converter like;
and change your bindings to;
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...