文章介绍了在WPF中使用ListView、ListBox和ItemsControl等控件时,如何在模板内绑定Button命令并传递参数。对于单个参数,可以使用CommandParameter绑定当前项。对于传递多个参数,可以通过实现IMultiValueConverter接口创建转换器并在Button的CommandParameter中使用MultiBinding。示例代码展示了具体实现方式。
摘要生成于
,由 DeepSeek-R1 满血版支持,
工作中突然遇到的两个问题:
一个就是LIstView 、ListBox、ItemsControl 控件里面有按钮,按钮就要传递参数,而这个参数就是实体。
另一个是传递参数的时候可能也会传递多个参数。
首先给看第一种:如何在模板中使用Button 按钮进行命令BinDing 与传递一个参数:
<
DataTemplate
x
:
Key
=
"DataTemplatetwo"
>
<
Border
Margin
=
"10"
BorderThickness
=
"1"
BorderBrush
=
"Blue"
Background
=
"Red"
Padding
=
"5"
>
<
StackPanel
Orientation
=
"Vertical"
>
<
TextBlock
Text
=
"{Binding name}"
Margin
=
"10"
>
<
/
TextBlock
>
<
TextBlock
Text
=
"{Binding score}"
Margin
=
"10"
>
<
/
TextBlock
>
<
TextBlock
Text
=
"{Binding grade}"
Margin
=
"10"
>
<
/
TextBlock
>
<
Button
Content
=
"通知学生家长"
Height
=
"40"
Width
=
"60"
Command
=
"{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},Path=DataContext.BtnCommand}"
CommandParameter
=
"{Binding}"
/
>
<
/
StackPanel
>
<
/
Border
>
<
/
DataTemplate
>
因为Button 定义在DataTemplate中,所以在模板中使用Button 时 需要绑定其DataContext,而DataContext又是ItemsControl给的,所以重点给按钮命令绑定方式:
<Button Content="通知学生家长" Height="40" Width="60" Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},Path=DataContext.BtnCommand}" CommandParameter="{Binding}"/>
CommandParameter:为传递的参数,基本上绑定到TreeView、ListBox、ListViewz中绑定的值类型是一个实体类。所以我们要的是所点击的那一项。通过这种传递参数的绑定方法,就可以实现传递参数。
当然还有其他的绑定数据传递方式:
如下:
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAnCestro,AncestorType=ListView},Path=SelectIrem}"
第二种:绑定命令与传递多个参数
这个时候我们就需要使用转换器:
看代码:
public class ButtonCommandOaramater : IMultiValueConverter
object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
return values.Clone();
object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
throw new NotImplementedException();
在前台中使用方式:
首先引用:
<Window.Resources>
<local:ButtonCommandOaramater x:Key="myButton"></local:ButtonCommandOaramater>
</Window.Resources>
程序中使用:
<DataTemplate x:Key="DataTemplatetwo" >
<Border Margin="10" BorderThickness="1" BorderBrush="Blue" Background="Red" Padding="5" >
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding name}" Margin="10"></TextBlock>
<TextBlock Text="{Binding score}" Margin="10"></TextBlock>
<TextBlock Text="{Binding grade}" Margin="10"></TextBlock>
<Button Content="通知学生" Height="40" Width="60" Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},Path=DataContext.BtnCommand}" >
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource myButton}">
<Binding></Binding>
<Binding RelativeSource="{RelativeSource Mode=Self}" Path="Content"></Binding>
</MultiBinding>
</Button.CommandParameter>
</Button>
</StackPanel>
</Border>
</DataTemplate>
后台代码:
public class MainWindowViewModel:ObservableObject
public ICommand BtnParanCommand { get; set; }
public MainWindowViewModel()
BtnParanCommand = new RelayCommand<Object>(BtnParanCommandExecute);
private void BtnParanCommandExecute(object obj)
var objArr = (object[])obj;
var per = objArr[0] as Person;
MessageBox.Show("姓名:" + per.name + " 成绩" + per.grade + " 点击了:" + objArr[1]);
因为是多参数,所以呢要使用数组,来通过这种方式来接收按钮的传递的参数的值。通过这种方式,可以让按钮传递的参数多达好几个。
OK ! 我给大家一个完整代码 链接:
https://download.csdn.net/download/weixin_43303279/87409871