将数据类型作为命令参数发送

时间:2014-01-28 14:34:02

标签: c# wpf itemssource commandparameter

我有一个列表视图,使用ItemsSource定义了哪些项目。 ItemsSource是本地班级类型的列表 我需要CommandParameter选择的项目DataType

有人可以帮我这个吗?

代码:(此代码位于App.xaml中,数据模板位于itemsTemplate标记内)

<Application.Resources>
    <DataTemplate x:Key="xxx" DataType="BL:DeviceInfo">
        <StackPanel>
            <Button Command="{Binding DataContext.SelectDeviceCommand RelativeSource={RelativeSource ancestorType=ListView}} CommandParameter="{???????}" />
        </StackPanel>
    </DataTemplate>
</Application.Resources>

我想将SelectedItem作为Deviceinfo发送,这是列表项的类型,它是列表视图的项目源。

1 个答案:

答案 0 :(得分:1)

最简单的是:

CommandParameter="{Binding}"

这会将DataContext对象本身作为命令参数发送。然后,您可以在命令的处理程序中派生它的类型。

如果您需要将类型本身作为参数发送,则需要创建转换器。

http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converter(v=vs.110).aspx

相关问题