WPF DataTemplate和Binding

我继续用MSDN的代码理解MVVC,我有一个问题。

在.xaml中,它们有一个显示在屏幕上的命令列表。

   

从这里,我了解DataContext已设置(此处未显示),它将显示命令集合。 我不明白的是你可以在下面看到的CommandsTemplate:

     pou         

如何创建绑定? 这段代码如何告诉从集合中的对象检查属性Command和DisplayName? 它来自ItemsSource吗? 如果是,我不明白为什么它只在{Binding}。 任何人都可以向我解释一下DataTemplate绑定如何从ContentTemplate运行?

如上所述,DataContext设置为ViewModel类,因此您在XAML中提到的控件将能够访问该ViewModel的公共属性。

例如:

 private ObservableCollection commands = new ObservableCollection(); public ObservableCollection Commands { get { return commands; } set { commands = value; } } 

Commander类的结构。

 public class Commander { public ICommand Command { get; set; } public string DisplayName { get; set; } } 

该VM具有名为Commands的属性,该属性可能是ObservableCollection。 可以从XAML访问此属性。

你可以想象HeaderedContentControl是一个容器。 HeaderedContentControl的内容是一个DataTemplate“CommandsTemplate”,它有一个ItemsControl,它绑定到VM的Commands属性。

Content =“{Binding Path = Commands}”

然后,您可以再次将ItemControl与Commands绑定,但ItemControl位于绑定到Commands的内容中。 因此,您无需再次指定路径。 你可以使用

  ItemsSource="{Binding}" instead of ItemsSource="{Binding Commands}". 

ItemControl中有两个文本块,因此它们与Commander ObservableCollection的Commander类处于同一级别。 这就是你可以直接访问Text =“{Binding Path = DisplayName}”的原因。

希望能帮助到你。

与{Binding}绑定的ItemsSource直接绑定到ItemsControl的DataContext(它将查找链,直到找到一个设置的DataContext)。 在这种情况下,它已在HeaderedContentControl中设置

然后,ItemsControl中的每个项目都将其DataContext设置为列表中的元素。

正在为列表中的每个Item设置模板,而不是为ItemsControl本身设置模板。 因此{Binding Path=Command}{Binding Path=DisplayName}将查看列表中元素的那些属性。

例:

XAML

       pou                

C#

 ///  /// Interaction logic for Window1.xaml ///  public partial class Window1 : Window { public Window1() { InitializeComponent(); Commands.Add(new Commander() { DisplayName = "DN1" }); Commands.Add(new Commander() { DisplayName = "DN2" }); Commands.Add(new Commander() { DisplayName = "DN3" }); this.DataContext = this; } private void Window_Loaded(object sender, RoutedEventArgs e) { } private ObservableCollection commands = new ObservableCollection(); public ObservableCollection Commands { get { return commands; } set { commands = value; } } } public class Commander { public ICommand Command { get; set; } public string DisplayName { get; set; } }