2011-04-18 72 views
0

希望它不是一个重复。我使用Silverlight 4如何在DataTemplate中绑定DataGrid.SelectedItem

我有一个集合的MyProjects从我的ViewModel MyVM绑定到DataGrid MainDataGrid

我也有一个RowDetailsTemplate来显示几个按钮。

我有命令绑定在这些按钮上。命令绑定Command =“{Binding Path = EditCommand}”似乎工作,但我不能让CommandParameter绑定工作。元素名称是否在DataTemplate中工作?通过DataTemplate中的CommandParameter绑定传递SelectedItem/SelectedDataRow的最佳方法是什么?

感谢您的帮助。

 <sdk:DataGrid x:Name="MainDataGrid" AutoGenerateColumns="False" DataContext="{StaticResource MyVM}" ItemsSource="{Binding MyProjects}" RowDetailsVisibilityMode="VisibleWhenSelected"> 

      <sdk:DataGrid.RowDetailsTemplate> 
       <DataTemplate> 
        <StackPanel Background="Ivory" Orientation="Horizontal"> 
         <Button Style="{StaticResource DataGridRowDetailsButtonStyle}" 
           Command="{Binding Path=EditCommand}" 
           CommandParameter="{Binding ElementName=MainDataGrid, Path=SelectedItem}">Edit</Button> 

回答

1

如何在视图模型(其中您定义EditCommand)中添加属性'MySelectedItem'并将其绑定到您的datagrid的selectedItem。

<sdk:DataGrid x:Name="MyDataGrid" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" SelectedItem="{Binding MySelectedItem}"> 

,那么你也许可以做到

CommandParameter="{Binding MySelectedItem}"