Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing a button on a selected datagridrow

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

like image 747
TheSjoerd Avatar asked Sep 06 '12 14:09

TheSjoerd


1 Answers

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>
like image 115
LPL Avatar answered Oct 23 '22 19:10

LPL