Multiple Parameter to pass to Command WPF [duplicate]

喜夏-厌秋 提交于 2019-12-03 00:39:36
Fredrik Hedblad

See this question
Passing two command parameters using a WPF binding

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

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();
    }
}
cjroebuck

I got this working without resorting to Attached Properties by using the x:Name property in the Xaml and then passing this on to my CommandParameter as a MultiBinding along with the Tag. From Front to Back:

In my View:

 <Button Content="{Binding PropertyA}" x:Name="PropertyA" Tag="{x:Type Types:TicketTypeA}" Style="{StaticResource LinkButton}"/>

 <Button Content="{Binding PropertyB}" x:Name="PropertyB" Tag="{x:Type Types:TicketTypeB}" Style="{StaticResource LinkButton}"/>

In the style for each button:

 <Style x:Key="LinkButton" TargetType="Button">
        <Setter Property="Command" Value="{Binding DataContext.ListHistoryCommand, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />

        <Setter Property="CommandParameter">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource propertyConverter}">
                    <MultiBinding.Bindings>
                        <Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}"/>
                        <Binding Path="Name" RelativeSource="{RelativeSource Mode=Self}"/>
                    </MultiBinding.Bindings>
                </MultiBinding>
            </Setter.Value>
        </Setter>

In my Converter:

public class PropertyConverter : IMultiValueConverter
{
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            //Type t = values[0] as Type;
            //String propName = values[1] as string;

            Type t = values[0] as Type;
            if (t == null)
                return typeof(TicketBase);
            String s = values[1] as String;

            return new Tuple<Type,String>(t,s);
        }
}

In my View Model:

private void ListHistory(object o)
    {
        if (Tickets.Count == 0)
            return;
        var tuple = o as Tuple<Type,String>;

        // Now write some code to dynamically select the propertyName (tuple.Item2) from the type (tuple.Item1)  

    }

I am now receiving the Type and PropertyName in my Command. Now, I just need to compile a lambda expression at runtime to dynamically Select the PropertyName from the Type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!