I have an XDocument like this one set as a DataContext of my Window:
Class MainWindow
Public Sub New()
InitializeComponent()
Me.DataContext = <?xml version="1.0" encoding="utf-8"?>
<Sketch Format="A4" Author="Aaron" Created="..." Test="Value">
<Item Kind="Line" X1="50" Y1="50" X2="150" Y2="150">
<Item Kind="Rect" X="10" Y="10" Width="30" Height="30"/>
</Item>
<Item Kind="Line" X1="250" Y1="250" X2="250" Y2="50">
<Item Kind="Ellipse" X="10" Y="10" Width="30" Height="30"/>
</Item>
<Test Param="Value"/>
</Sketch>
End Sub
End Class
Now in my frontend I test couple of different binding paths. All of them works with Elements, Element, Attribute, but Attributes doesn’t seem to work for me. I consider it rather odd, because Elements is IEnumerable<XElement> and Attributes is IEnumerable<XAttribute> — exactly the same kind of collection and everything.
<Window Height="320" Title="Main Window" Width="640" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="MainWindow">
<UniformGrid Columns="3">
<StackPanel>
<Label Foreground="DimGray">Root.Elements.Count</Label>
<Label Content="{Binding Path=Root.Elements.Count, FallbackValue=Loading…}"/>
<Label Foreground="DimGray">Root.Attributes.Count</Label>
<Label Content="{Binding Path=Root.Attributes.Count, FallbackValue=Loading…}"/>
<Label Foreground="DimGray">Root.Element[Test]</Label>
<Label Content="{Binding Path=Root.Element[Test], FallbackValue=Loading…}"/>
<Label Foreground="DimGray">Root.Attribute[Test]</Label>
<Label Content="{Binding Path=Root.Attribute[Test], FallbackValue=Loading…}"/>
</StackPanel>
<StackPanel>
<Label Foreground="DimGray">Root.Elements</Label>
<ListBox ItemsSource="{Binding Root.Elements}"/>
<Label Foreground="DimGray">Root.Attributes</Label>
<ListBox ItemsSource="{Binding Root.Attributes}"/>
</StackPanel>
<StackPanel>
<TreeView ItemsSource="{Binding Root.Elements}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Elements}">
<Label Content="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</StackPanel>
</UniformGrid>
</Window>
Do you have any idea why everything binds correctly except Attributes? Any help is appreciated. I think is has (maybe) got something to do with a fact, that Element and Elements are inherited from XContainer, but this doesn’t explain why XElements very own Attribute works…
Thanks in advance!
Aaron
There is no property
AttributesonXElement(only methodAttributes()that can’t be used directly in binding), so it’s not surprising the binding doesn’t work.But there is also no property
Elements, so why does that work? It’s because LINQ to XML objects have special “dynamic properties” specifically for use in WPF, see LINQ to XML Dynamic Properties. There is a dynamic propertyElementsonXElement, but noAttributes.There’s still one thing I don’t understand though: The
Elementsdynamic property is documented to work only in the formelem.Elements[elementName]. So it’s still surprising to me that your code works.If you want to know about any workarounds, I can’t think of any, except for invoking the
Attributes()method using<ObjectDataProvider>.