在选定的数据网格行上显示一个按钮。

7

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

2个回答

12

它不起作用是因为依赖属性值优先级。你不能在一个Style中更改本地值。把Visibility.Hidden移到Style中就可以了。

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

2
你可以使用WPF提供的转换器BooleanToVisibiltyConverter来切换按钮的可见性。
<DataGrid>
            <DataGrid.Resources>
                <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="50">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="X" Tag="{Binding}"
                                    Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter},
                                                    RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

谢谢您的建议。如果有其他方法可以完成任务,我会尽量避免使用转换器。也许这个转换器以后还能用到别的地方。 - TheSjoerd
是的,我也避免编写转换器,但在这里我没有编写一个。使用WPF本身提供的一些默认转换器。 :) - Rohit Vats

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接