Элементы и FirstAttribute связываются так, как я ожидал (если бы я не знал, что это метод), а Attributes - нет, несмотря на то, что он является членом XElement, как и другие. Я знаю о IValueConverter, и я использую это, чтобы получить связывание, которое я хочу для атрибутов, но мне любопытно, почему оно работает на Elements.
<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBlock Text="{Binding Path=FirstAttribute}" />
<ListBox ItemsSource="{Binding Path=Elements}" />
<ListBox ItemsSource="{Binding Path=Attributes}" />
</StackPanel>
</Window>
using System.Linq;
using System.Windows;
using System.Xml.Linq;
namespace WpfApplication6 {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
XDocument doc = new XDocument(
new XElement("Parent",
new XAttribute("attr1", "val"),
new XAttribute("attr2", "val"),
new XElement("Child1"),
new XElement("Child2")
)
);
MessageBox.Show("Elements: " + doc.Elements().First().Elements().Count());
MessageBox.Show("Attributes: " + doc.Elements().First().Attributes().Count());
DataContext = doc.Elements().First();
}
}
}