Reputation: 160
I'm using the MVVM pattern. I've bound my items and I want to only show the edit button when a row is selected in the datagrid. It appears to be possible with triggers in WPF but we don't have triggers in Silverlight. I tried a TemplatedParent binding but I'm not sure what the TemplatedParent is in this case. We don't have RelativeSource ancestor in Silverlight either. At this point I'm going to look at a solution using the code behind...
<data:DataGrid.Columns>
<data:DataGridTemplateColumn IsReadOnly="True" Header="Name" Width="300">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock x:Name="textBlock" Text="{Binding Name, Mode=OneWay}" VerticalAlignment="Center" Margin="4,4,0,4"/>
<Button Margin="1,1,4,1" HorizontalAlignment="Right" VerticalAlignment="Center" Padding="7,4" Content="Edit" />
</Grid>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
Upvotes: 0
Views: 1076
Reputation: 16894
If only Silverlight had a RelativeSource FindAncestor binding...
One kind-of hacky idea I could suggest would be to put your editing controls into a RowDetailsTemplate on the DataGrid itself, then set the RowDetailsVisibilityMode to VisibleWhenSelected.
It might not be what you're after look-wise, but it probably "solves" your particular use-case.
If it doesn't, then I'd probably violate MVVM here (very carefully). Usually DataGrids are the bastard child edge case; they almost all need some variety of code-behind.
Upvotes: 0
Reputation: 3994
There are a couple ways you could do this in silverlight, although I don't think any of them can be pure XAML solutions. With MVVM, you then create property in your view model that you bind to the SelectedItem property of the DataGrid. From there, there are two differnt options:
Upvotes: 1