I have a WPF datagrid that has two associated buttons for editing and deleting data. Ideally, I’d like to disable or make these buttons invisible when an item is not selected in the grid. How do I approach this?
If it matters my datagrid XAML is:
<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>
You can use DataTrigger on button to handle the disable/visible of buttons OR you can write the logic on CanExecuteChange event of command which gets binded to button
Data Trigger
Command
XAML