I have a datagrid with a number ow rows. Every row has as DeleteRow-button. Only the row that is selected should have this button visible. As I see it there may be at least two solutions:
a) binding the Visibility-property of the button to the IsSelected-property of the containing DatGridRow
or
b) using a trigger in the button to only be visible when the containing row is selected.
This is the code I have for option b, which isn't working:
<DataGridTemplateColumn Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="X" Tag="{Binding}" Click="DeletRow_Click" Visibility="Hidden">
<Button.Style>
<Style x:Name="ButtonVisibility">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}},Path=IsSelected}" Value="True">
<Setter Property= "Button.Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
It's probably really easy but I've been staring so much it's blinding me now :S
Thanks
It doesn't work because of Dependency Property Value Precedence. You can't change a local value within a Style
. Move Visibility.Hidden
into Style
and it will work.
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="X" Tag="{Binding}" Click="DeletRow_Click">
<Button.Style>
<Style x:Name="ButtonVisibility">
<Setter Property="Button.Visibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=IsSelected}" Value="True">
<Setter Property="Button.Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With