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}"
相关推荐
Scout-leaf4 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
柒.梧.6 天前
基于SpringBoot+JWT 实现Token登录认证与登录人信息查询
wpf
十月南城9 天前
Flink实时计算心智模型——流、窗口、水位线、状态与Checkpoint的协作
大数据·flink·wpf
听麟12 天前
HarmonyOS 6.0+ 跨端会议助手APP开发实战:多设备接续与智能纪要全流程落地
分布式·深度学习·华为·区块链·wpf·harmonyos
@hdd12 天前
Kubernetes 可观测性:Prometheus 监控、日志采集与告警
云原生·kubernetes·wpf·prometheus
zls36536512 天前
C# WPF canvas中绘制缺陷分布map
开发语言·c#·wpf
专注VB编程开发20年12 天前
c#Redis扣款锁的设计,多用户,多台电脑操作
wpf
闲人编程13 天前
定时任务与周期性调度
分布式·python·wpf·调度·cron·定时人物·周期性
zls36536513 天前
C# WPF canvas中绘制缺陷分布map并实现缩放
开发语言·c#·wpf
数据知道14 天前
PostgreSQL:Citus 分布式拓展,水平分片,支持海量数据与高并发
分布式·postgresql·wpf