WPF 菜单事件绑定 DataTemplate下button Command事件绑定 DataTemplate遍历实体数据

原文: WPF 菜单事件绑定 DataTemplate下button Command事件绑定 DataTemplate遍历实体数据

一、菜单编写

不要使用listbox,listbox只有selectchanged事件,没有点击事件,会导致,点相同菜单没有触发事件的情况

1、引用部分增加“Name="frmWorkstation" ”

2、绑定command时候,必须写成<Button Command="{Binding DataContext.AddTabItemCommand,ElementName=frmWorkstation}" CommandParameter="{Binding Param}",否则ItemTemplate的DataContext指代不明,需要改为父类的DataContext。


  
  
  1. <ItemsControl ItemsSource= "{Binding MenuList}">
  2. <ItemsControl.ItemTemplate>
  3. <DataTemplate>
  4. <Button Command= "{Binding DataContext.AddTabItemCommand,ElementName=frmWorkstation}" CommandParameter= "{Binding Param}" Padding= "0" BorderThickness= "0" Width= "168" Height= "35" HorizontalContentAlignment= "Left" Background= "Transparent" Cursor= "Hand">
  5. <DockPanel >
  6. <Image Width= "16" Height= "16" Source= "{Binding Icons}"/>
  7. <TextBlock Margin= "6,0,0,0" Text= "{Binding Title}"/>
  8. </DockPanel>
  9. </Button>
  10. </DataTemplate>
  11. </ItemsControl.ItemTemplate>
  12. </ItemsControl>

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/12164912.html