首页 > 代码库 > WPF – pass multiple parameters to a Command
WPF – pass multiple parameters to a Command
public class SendCommand : ICommand{ public void Execute(object parameter) { var labels = ((object[]) parameter).OfType<Label>(); } public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged = delegate {};} public partial class MainWindow : Window{ public MainWindow() { InitializeComponent(); DataContext = this; SendCommand = new SendCommand(); } public ICommand SendCommand { get; set; }} public class PassThroughConverter : IMultiValueConverter{ public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { return values.ToArray(); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); }}
<Window x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfApplication5="clr-namespace:WpfApplication5" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <wpfApplication5:PassThroughConverter x:Key="PassthroughConverter" /> </Window.Resources> <Grid> <StackPanel> <Label Name="Greeting">Hello</Label> <Label Name="Farewell">Goodbye</Label> <Button Command="{Binding SendCommand}">Hit me <Button.CommandParameter> <MultiBinding Converter="{StaticResource PassthroughConverter}"> <Binding Path="." ElementName="Greeting"></Binding> <Binding Path="." ElementName="Farewell"></Binding> </MultiBinding> </Button.CommandParameter> </Button> </StackPanel> </Grid></Window>
WPF – pass multiple parameters to a Command
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。