2009-06-10 46 views
1

元素和FirstAttribute綁定正如我所料(如果我不知道它是一種方法),但屬性不會像其他人一樣,儘管是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(); 
     } 
    } 
} 

回答

0

您確定Elements是否有效?因爲據我所知,你不能直接綁定到一個方法。元素和屬性都是方法,爲了解決這個問題,請參見this question

+0

這就是我想,太,但直接綁定到元素確實有效。 我沒有求助於一個IValueConverter屬性。 – lesscode 2009-06-10 21:07:21

相關問題