传递给命令 WPF 的多个参数 [英] Multiple Parameter to pass to Command WPF

查看:49
本文介绍了传递给命令 WPF 的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下层次结构:

abstract class TicketBase
{
    public DateTime PublishedDate { get; set; }
}

class TicketTypeA:TicketBase
{
     public string PropertyA { get; set; }
}   

class TicketTypeB:TicketBase
{
     public string PropertyB { get; set; }
}

在我的虚拟机中,我有一个 List<TicketBase>门票.当用户单击我的应用程序上的按钮时,他们希望查看某个属性的 previous 值列表,例如:

In my VM I have a List<TicketBase> Tickets. When a user clicks a button on my app, they want to see a list of previous values of a certain property, e.g.:

<Button Tag="{x:Type Types:TicketTypeA}" 
        Command="{Binding ListHistoryCommand}"
        CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" />

如您所见,我将 Tag 属性设置为 TicketTypeA 并将其作为参数传递给我的命令:

as you can see, I set my Tag property to TicketTypeA and pass that as parameter to my command:

private void ListHistory(object o)
{
   if (Tickets.Count == 0)
       return;
   Type ty = o as Type;
   ValueHistory = new ObservableCollection<TicketBase>(GetTicketsOfType(ty).Select(t => t)); // <- Need to return t.PropertyA here, but dynamically
}

IEnumerable<TicketBase> GetTicketsOfType(Type type)
{
    if (!typeof(TicketBase).IsAssignableFrom(type))
        throw new ArgumentException("Parameter 'type' is not a TicketBase");
    return Tickets.Where(p => p.GetType() == type);
}

(ValueHistory 是我在网格上设置为 ItemsSource 的另一个集合)

(ValueHistory is another collection that I set as ItemsSource on my grid)

但是我还需要传入 property 名称,这样我就可以像这样在网格中显示该属性:

However I need to also pass in the property name too, so that I can display just that property in the grid like so:

Published Time     |  PropertyA
===================================================
09:00              | <value of PropertyA at 09:00>
08:55              | <value of PropertyA at 08:55>

所以问题基本上是将属性名称作为另一个参数传递给我的命令的最干净的方法是什么?

So the question is basically what is the cleanest way to pass in the property name as another parameter into my command?

推荐答案

看到这个问题
使用 WPF 绑定传递两个命令参数

更新
如果您需要在 Button 上同时存储类型和属性名称,则必须像您说的那样使用附加属性.要将这两个参数传递给命令,这样的事情应该可以工作

Update
If you need to store both the Type and the Property Name on the Button you'll have to use an attached property like you said. To pass the two parameters to the Command, something like this should work

<Button Tag="{x:Type Types:TicketTypeA}"
        local:ParameterNameBehavior.ParameterName="{Binding Source='Parameter A'}"
        Command="{Binding ListHistoryCommand}">
    <Button.CommandParameter>
        <MultiBinding Converter="{StaticResource PassThroughConverter}">
            <Binding Path="Tag" RelativeSource="{RelativeSource Self}"/>
            <Binding Path="(local:ParameterNameBehavior.ParameterName)"
                     RelativeSource="{RelativeSource Self}"/>
        </MultiBinding>
    </Button.CommandParameter>
</Button>

参数名称行为

public static class ParameterNameBehavior
{
    private static readonly DependencyProperty ParameterNameProperty = 
        DependencyProperty.RegisterAttached("ParameterName",
                                            typeof(string),
                                            typeof(ParameterNameBehavior));
    public static void SetParameterName(DependencyObject element, string value)
    {
        element.SetValue(ParameterNameProperty, value);
    }
    public static string GetParameterName(DependencyObject element)
    {
        return (string)element.GetValue(ParameterNameProperty);
    }
}

PassThroughConverter

public class PassThroughConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values.ToList();
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

这篇关于传递给命令 WPF 的多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆