代码之家  ›  专栏  ›  技术社区  ›  lesscode

为什么在这种情况下其中一条路径不绑定?

  •  1
  • lesscode  · 技术社区  · 17 年前

    元素和FirstAttribute按照我的预期绑定(如果我不知道这是一个方法的话),但是属性没有绑定,尽管它是XElement的成员,就像其他属性一样。我知道IValueConverter,我用它来获得我想要的属性绑定,但我很好奇为什么它能在元素上工作。

    <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();
            }
        }
    }
    
    2 回复  |  直到 14 年前
        1
  •  0
  •   Community Mohan Dere    9 年前

    你确定元素有效吗?因为据我所知,您不能直接绑定到方法。元素和属性都是解决此问题的方法,请参见 this question .

        2
  •  0
  •   lesscode    17 年前

    在MSDN上得到了答案:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4be39d21-c9d8-4781-9337-2cb1215ec3d1/

    基本上,XAML团队向XLinq添加了专门用于绑定的PropertyDescriptors,但肯定忘记了属性。。。

    推荐文章