开发者

How to bind DataGrid.SelectedItem in DataTemplate

Hope it's not a dup. I'm using Silverlight 4.

I have a collection MyProjects from my ViewModel MyVM binding to a datagrid MainDataGrid.

I also have a RowDetailsTemplate to show several buttons.

I have Command binding on these buttons. The command binding Command="{Binding Path=EditCommand}" appears working but I just can't get the CommandParameter binding working. Is Element name working inside a DataTemplate? What's the best 开发者_如何学Goway to pass in SelectedItem/SelectedDataRow via a CommandParameter binding inside a DataTemplate?

Thanks for your help.

        <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>


How about adding a property 'MySelectedItem' in the view model (where you define your EditCommand) and make it bound to your datagrid's selectedItem.

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

then you can probably do

CommandParameter="{Binding MySelectedItem}"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜