我有一个WPF数据网格,它有两个相关的按钮,用于编辑和删除数据。理想情况下,我想在网格中未选择项目时禁用或使这些按钮不可见。我该如何处理?
如果重要的是我的数据网格XAML是:
<DataGrid AutoGenerateColumns="True" Margin="10,174,12,35" Name="dataGridArchiveQueue" Visibility="Visible" AlternatingRowBackground="#01000000" BorderBrush="#FF688CAF"
HorizontalGridLinesBrush="#37000000" VerticalGridLinesBrush="#37000000" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True"
SelectedItem="{Binding SelectedItemArchiveGrid}" Grid.ColumnSpan="2">
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="LightBlue" />
</DataGrid.Resources>
</DataGrid>
答案 0 :(得分:2)
您可以使用按钮上的DataTrigger来处理按钮的禁用/可见按钮,或者您可以在命令的CanExecuteChange事件中编写逻辑,该命令被绑定到按钮
数据触发
<Button.Style> <Style> <Style.Triggers> <DataTrigger Binding="{SelectedItemArchiveGrid}" Value="{x:Null}"> <Setter Property="IsEnabled"Value="False"></Setter> </DataTrigger> </Style.Triggers> </Style> </Button.Style>
<强>命令强>
> public RelayCommand<object> DeletCommand { get; set; }
>
> DeletCommand = new RelayCommand<object>(OnDelete, OnDeletCanExecute);
>
> private void OnDelete(object obj) { }
>
> private bool OnDeletCanExecute(object obj) {
return SelectedItemArchiveGrid != null; }
<强> XAML 强>
<Button Content="Delete" Command="{Delete Command}"/>
答案 1 :(得分:0)