WPF MVVM下 ItemsControl条目命令绑定传参

xaml:

XML 复制代码
  <ItemsControl x:Name="itemsControl" ItemsSource="{Binding TreeViewModelForFlowList}">
      <!--  定义ItemsPanel,设置项之间的间距  -->
      <ItemsControl.ItemsPanel>
          <ItemsPanelTemplate>
              <StackPanel Margin="10" Orientation="Vertical" />
          </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>

      <!--  定义项模板  -->
      <ItemsControl.ItemTemplate>
          <DataTemplate>
              <!--  每个项的外边框  -->
              <Border
                  Margin="0,2"
                  Padding="8"
                  BorderThickness="1"
                  CornerRadius="4">
                  <Grid>
                      <Grid.ColumnDefinitions>
                          <ColumnDefinition Width="*" />
                          <!--  第一个按钮占剩余空间  -->
                          <ColumnDefinition Width="Auto" />
                          <!--  第二个按钮自适应内容  -->
                          <ColumnDefinition Width="5" />
                          <!--  间距  -->
                          <ColumnDefinition Width="Auto" />
                          <!--  第三个按钮自适应内容  -->
                      </Grid.ColumnDefinitions>

                      <!--  第一个大按钮  -->
                      <telerik:RadButton
                          Grid.Column="0"
                          Height="40"
                          Margin="0,0,5,0"
                          Command="{Binding DataContext.FlowControlCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                          CommandParameter="{Binding}"
                          Content="{Binding FlowDescription}"
                          FontSize="14"
                          FontWeight="SemiBold"
                          Foreground="White" />

                      <!--  第二个小按钮  -->
                      <Button
                          Grid.Column="1"
                          Width="60"
                          Height="30"
                          Background="#6C757D"
                          Content="{Binding TreeViewActionModel}"
                          FontSize="12"
                          Foreground="White" />
                  </Grid>
              </Border>
          </DataTemplate>
      </ItemsControl.ItemTemplate>
  </ItemsControl>

界面:

命令方法:

cs 复制代码
  private ObservableCollection<TreeViewModelForFlow> _treeViewModelForFlowList;

 /// <summary>
 /// 按钮界面配置列表
 /// </summary>
 public ObservableCollection<TreeViewModelForFlow> TreeViewModelForFlowList
 {
     get => _treeViewModelForFlowList;
     set { this.RaiseAndSetIfChanged(ref _treeViewModelForFlowList, value); }
 }

public void FlowControl(TreeViewModelForFlow flow)
{
    
}

要点,直接绑定命令会找不到,直接写命令绑定是在ItemsControl的每个项中绑定的,就像按钮的Content绑定一样,绑定的是每个项的属性,但想要的效果实际上是在主ViewModel中,需要使用 RelativeSource 找到父级DataContext,即为主ViewModel。

或者使用 ElementName 绑定

XML 复制代码
Command="{Binding ElementName=itemsControl, Path=DataContext.FlowControlCommand}"
相关推荐
lingxiao168887 小时前
WebApi详解+Unity注入--下篇:Unity注入
unity·c#·wpf
无心水18 小时前
【分布式利器:腾讯TSF】6、TSF可观测性体系建设实战:Java全链路Metrics+Tracing+Logging落地
java·分布式·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
故事不长丨1 天前
C#字典(Dictionary)全面解析:从基础用法到实战优化
开发语言·c#·wpf·哈希算法·字典·dictionary·键值对
冰茶_2 天前
WPF路由事件:隧道与冒泡机制解析
学习·c#·.net·wpf·.netcore·mvvm
He BianGu2 天前
【笔记】 WPF中CollectionChangedEventManager功能详细介绍
笔记·wpf
张人玉2 天前
C#WPF页面布局及其属性
开发语言·c#·wpf
故事不长丨2 天前
C#集合:解锁高效数据管理的秘密武器
开发语言·windows·c#·wpf·集合·winfrom·字典
故事不长丨2 天前
C#队列深度剖析:解锁高效编程的FIFO密码
visualstudio·c#·wpf·多线程·winfrom·队列·queue
lhrimperial3 天前
微服务架构深度解析-微服务理论基础(一)
微服务·架构·wpf
艾斯比的日常3 天前
XXL-Job 核心原理深度解析
wpf