WPF Command 传递多个参数 有时候Command需要多个参数,记录一下操作方式 <ButtonContent="Zoom"Command="{Binding BtnCommand"><Button.CommandParameter><MultiBindingConverter="{StaticResource MyConverter}"><BindingPath="Width"ElementName="Canvas1"/><BindingPath="Height"ElementName="Canvas1"/></MultiBinding></...
传递单个参数相对简单,只需在XAML中直接设置 CommandParameter 即可。例如: xml <Button Command="{Binding MyCommand}" CommandParameter="SomeParameter" Content="Click Me"/> 在上面的例子中,MyCommand 是ViewModel 中定义的一个命令,而 "SomeParameter" 就是传递给该命令的参数。
如果需要传递多个参数,可以将多个参数封装成一个对象进行传递。命令用 RelayCommand<T>泛型。 ▲ 点击“传递按钮”,将左边的 TextBox 内容传递到右边 TextBlock 中去。 XAML: <Window.DataContext> <local:MainVM/> </Window.DataContext> <Grid> <Grid.Resources> <local:UserInfoConvert x:Key="...
<Grid><ButtonContent="测试按钮"Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=DataContext.YourCommand}"CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}"></Button><!--用于给点击按钮时,传递多个参数--><Gridx:Name="studentIdGrid"Tag="{Binding stude...
一般写法,一般只能传递一个参数 <i:Interaction.Triggers> <i:EventTrigger EventName="LostFocus"> <i:InvokeCommandAction Command="{Binding DataContext.LostFocusCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" PassEventArgsToCommand="True" /> ...
首先,如果您正在执行 MVVM,您通常可以通过与视图绑定的单独属性为您的 VM 提供此信息。这使您不必将任何参数传递给您的命令。 但是,您也可以多重绑定并使用转换器来创建参数: <ButtonContent="Zoom"Command="{Binding MyViewModel.ZoomCommand"><Button.CommandParameter><MultiBindingConverter="{StaticResource YourConve...
<Button Command="{Binding PassArgObjCmd}" Content="传递多个参数" Height="23" HorizontalAlignment="Left" Width="100"> <Button.CommandParameter> <local:UserParam UserName="悟空" UserPhone="110" UserAdd="花果山" UserSex="男" ></local:UserParam> ...
/// <summary>/// 用于从Xml中AddCoilCommand事件命令传递多参数转换/// </summary>classMultiValueConverter:IMultiValueConverter{publicobjectConvert(object[]values,TypetargetType,objectparameter,CultureInfoculture){stringplcId=values[0]asstring;ObservableCollection<CoilsInfo>coilList=values[1]asObservableCollectio...
您可以尝试使用自定义Converter and MultiBinding
WPF之Command基础:[3]命令参数 简介 命令库中的大多数现有命令不使用命令参数(CommandParameter)。 在使用命令参数的命令中,大多数命令的参数都具有某些基元类型值,如整数或字符串。通过命令参数,可以使用相同的命令来处理不同的任务,譬如不同的按钮使用相同的命令,但是命令参数不一样便可区分由哪个按钮触发。下面...