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

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

问题描述


可能重复:

使用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; }
}



在我的VM中,我有一个 List< TicketBase> ;门票。当用户点击我的应用上的按钮时,他们想查看某个属性的之前的值列表,例如:

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}}" />

你可以看到,我设置了标签属性到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)

但我还需要传入属性名称,以便我可以在网格中只显示该属性:

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绑定传递两个命令参数

更新

如果您需要在按钮上存储类型和属性名称,则必须使用附加属性像你说的。要传递这两个参数到命令,这样应该工作

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>

ParameterNameBehavior

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 >

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天全站免登陆