C# WPF传递多个参数,包括对象

C# WPF传递多个参数,包括对象,c#,wpf,data-binding,parameters,C#,Wpf,Data Binding,Parameters,我有一个简单的按钮,它可以执行特定的操作,比如编辑元素。我想传递一些参数,包括从datagrid专门选择的object Match和从callendar选择的SelectedDate。现在我有这样的东西: <DatePicker x:Name="dpEditDateMatch" SelectedDateFormat="Long" SelectedDate="{Binding MyDateTimeProperty2, Mode=TwoWay}" ></DatePicker>

我有一个简单的按钮,它可以执行特定的操作,比如编辑元素。我想传递一些参数,包括从datagrid专门选择的object Match和从callendar选择的SelectedDate。现在我有这样的东西:

<DatePicker x:Name="dpEditDateMatch" SelectedDateFormat="Long" SelectedDate="{Binding MyDateTimeProperty2, Mode=TwoWay}" ></DatePicker>
                    <Button Content="Edit match" Command="{Binding EditMatchCommand}">
                        <Button.CommandParameter>
                            <MultiBinding Converter="{StaticResource Converter}">
                                <Binding >??</Binding>
                                <Binding Path="SelectedDate" ElementName="dpEditDateMatch"/>
                            </MultiBinding>
                        </Button.CommandParameter>
                    </Button>
                    <ScrollViewer>
                        <DataGrid IsReadOnly="True" ItemsSource="{Binding match}" SelectedItem="{Binding SelectedMatch}"/>
                    </ScrollViewer>

我该怎么做?我的意思是,从xaml处理这个问题。

我刚刚遇到了你昨天说的问题。

我现在就解决了。

以下是XAML:

<Page.Resources>
            <local:MultiIcommandParameterConverter x:Key="MultiIcommandParameterConverter"></local:MultiIcommandParameterConverter>
    </Page.Resources>
 <ItemsControl>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel></WrapPanel>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Button Command="{Binding ButtonClick}">
                                        <Button.CommandParameter>
                                            <MultiBinding Converter="{StaticResource MultiIcommandParameterConverter}">
                                                <Binding Path="DetailIndex"/>
                                                <Binding Path="DetailContent"/>
                                            </MultiBinding>
                                        </Button.CommandParameter>                                       
                                    </Button>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>









下面是关于IMultiValueConverter的代码,最重要的是:

public class MultiIcommandParameterConverter : IMultiValueConverter
{        
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values[0] + "," + values[1];
    }

    object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}











使用“,”将所有参数转换为一个参数进行拆分。单击按钮时,按“,”将参数拆分,'转换为参数。

您的
视图模型中已经有了数据,即
MyDateTimeProperty2
用于日期,而
用于匹配。在多重绑定中,应传递以下消息:

<Button Content="Edit match" Command="{Binding EditMatchCommand}">
    <Button.CommandParameter>
        <MultiBinding Converter="{StaticResource Converter}">
            <Binding Path="MyDateTimeProperty2" />
            <Binding Path="SelectedMatch" />
        </MultiBinding>
    </Button.CommandParameter>
</Button>  

因为按钮已经具有与日期选择器和数据网格相同的
DataContext
,您甚至不需要
DatePicker上的
x:Name=“dpEditDateMatch”
来获取数据

public class MultiIcommandParameterConverter : IMultiValueConverter
{        
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values[0] + "," + values[1];
    }

    object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<Button Content="Edit match" Command="{Binding EditMatchCommand}">
    <Button.CommandParameter>
        <MultiBinding Converter="{StaticResource Converter}">
            <Binding Path="MyDateTimeProperty2" />
            <Binding Path="SelectedMatch" />
        </MultiBinding>
    </Button.CommandParameter>
</Button>