Selecting a certain row from a dataset
I have a Dataset and want to display the containing rows in a WPF-Contorl. The problem is, that the control is sort of a circuit diagram. Thus I created a template to show the values and placed multiple instances of it in my circuit control. Currently the XAML-code in the circuit looks like this:
&l开发者_StackOverflowt;Label Content="{Binding Path=.[0]}" ContentTemplate="{StaticResource ValueTpl}" />
<Label Content="{Binding Path=.[1]}" ContentTemplate="{StaticResource ValueTpl}" />
And so on. So I'm able to show the n-th row at a specified position. The problem is, that I can't expect the right order anymore and I would rather need something like:
<Label Content="{Binding Path=.[id=5]}" ContentTemplate="{StaticResource ValueTpl}" />
<Label Content="{Binding Path=.[id=8]}" ContentTemplate="{StaticResource ValueTpl}" />
I read that XPath ought to be supported, but I can't get this to work.
It is supported if you type XPath
instead of Path
...
Yes, I need to use XPath
, but there's more to it.
My mistakes were:
You can't use a DataSet
directly but need to wrap it in a XmlDataDocument
Also my problem involved the namespaces. To surpress them you have to set DataSet.Namespace = String.Empty
prior to creating the XmlDataDocument
.
If you want to use Namespaces you have to create a XmlNamespaceMappingCollection
in XAML like this
<UserControl.Resources>
<XmlNamespaceMappingCollection x:Key="namespace">
<XmlNamespaceMapping Prefix="ds" Uri="http://tempuri.org/DataSet.xsd" />
</XmlNamespaceMappingCollection>
</UserControl.Resources>
In order to reference the namespace in the XPath. Simply adding it as xmlns won't work (as opposed to what I expected).
Then referencing a certain row worked like this:
<Label Content="{Binding XPath='//TableName[4]'}" />
If you use namespaces, you need to reference the XmlNamespaceManager
<Label Content="{Binding XPath='//ds:TableName[4]'}" Binding.XmlNamespaceManager="{StaticResource namespace}" />
Now while this very simple XPath works, adding a constraint will lead to full CPU usage and my program not continuing:
<Label Content="{Binding XPath='//TableName[Process = 4]'}" />
or
<Label Content="{Binding XPath='//ds:TableName[ds:Process = 4]'}" Binding.XmlNamespaceManager="{StaticResource namespace}" />
EDIT
It seems like the binding must be in OneTime
Mode. Simply changing it to
<Label Content="{Binding Mode=OneTime, XPath='//TableName[Process = 4]'}" />
made it work. I also use a ContentTemplate
and in the template the binding might be TwoWay
, but there the XPath is primitive (e.g. ./Prozess
) , maybe thats why.
精彩评论