开发者

Silverlight - Access the Layout Grid's DataContext in a DataGrid CellTemplate's DataTemplate?

I am using Silverlight 3 to develop an application. In my app, I have a layout Grid (named "LayoutGrid") in which I have a DataGrid (named "PART_datagrid") with DataGridTemplateColumns. The LayoutGrid is set a DataContext in which there is a Ladders list as a property. This Ladders list is set as the ItemsSource for the PART_datagrid.

<Grid x:Name="LayoutRoot">
   <DataGrid x:Name="P开发者_如何学GoART_datagrid" ItemsSource="{Binding Ladders}">
      ...
      <DataGridTemplateColumn>
         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
               <Button Name="DeleteLadder" Click.Command="{Binding ElementName=LayoutRoot, Path=DataContext.DeleteLadderCommand}" />

Now in one of the DataGridTemplateColumns I have a button which should invoke a Command thats present in the LayoutGrid's DataContext. So I tried Element-To-Element binding on my DataTemplate button as follows

<Button Name="DeleteLadder" Click.Command="{Binding ElementName=LayoutRoot, Path=DataContext.DeleteLadderCommand}" />

But this does not seem to work. What I want to achieve is to handle the event of deletion of a DataGrid row at the parent DataContext level using the command.

Can someone pls suggest how do I proceed on this?

Thanks in advance...


The problem seems to be that each row uses the datagrid source as its "new" datacontext kind of thing. So from each row you need to get out of the grid and point to someting higher in the hierarchy to get the parent datacontext. These solutions may help. Solution 2 worked for me when I encountered the same problem.

Solution 1 using the Locator

See this posting: Silverlight DataGrid.Celltemplate Binding to ViewModel

Solution 2 using define resource at the top and hooking up to its datacontext.

<UserControl.Resources>
    <ContentControl x:Key="cc1" Content="{Binding}" />
</UserControl.Resources>

then use something like this inside your datagrid

Command="{Binding Source={StaticResource cc1}, Path=Content.DeleteLadderCommand}"

Good Luck

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜