The goal is to insert an enabled Button
into a column of a DataGrid
.
In the following XAML code snippet, the Button
is inserted into the DataGrid
with the IsEnabled
property set to true.
<DataGrid Name="DgData" HorizontalAlignment="Left" Height="306" Margin="10,119,0,0" VerticalAlignment="Top" Width="637" Background="White">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsEnabled" Value="{Binding RelativeSource={RelativeSource Self},Path=IsNewItem,Mode=OneWay}" />
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Id" IsReadOnly="True" Visibility="Hidden"/>
<DataGridTextColumn Header="Date" Binding="{Binding Date}" IsReadOnly="True" Width="100" CanUserResize="False"/>
<DataGridTextColumn Header="Domain name" Binding="{Binding DomainName}" IsReadOnly="True" Width="105" CanUserResize="False"/>
<DataGridTextColumn Header="Type" Binding="{Binding Relevance}" IsReadOnly="True" Width="65" CanUserResize="False"/>
<DataGridTextColumn Header="Category" Binding="{Binding DomainName}" IsReadOnly="True" Width="65" CanUserResize="False"/>
<DataGridTextColumn Header="Description" Binding="{Binding Content}" IsReadOnly="True" Width="200" CanUserResize="False"/>
<DataGridTemplateColumn MinWidth="100" Header="Note" CanUserSort="True" CanUserResize="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="BtnShowNotes" IsEnabled="True" Click="BtnShowNotes_OnClick">Show</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
However, when executing the application, the button is disabled.
What is causing the issue?
Your RowStyle
sets the IsEnabled
property of the entire row to false
. Remove this one and the Button
should be enabled:
<DataGrid Name="DgData" HorizontalAlignment="Left" Height="306" Margin="10,119,0,0" VerticalAlignment="Top" Width="637" Background="White">
<DataGrid.Columns>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Id" IsReadOnly="True" Visibility="Hidden"/>
<DataGridTextColumn Header="Datum" Binding="{Binding Date}" IsReadOnly="True" Width="100" CanUserResize="False"/>
<DataGridTextColumn Header="Doménové jméno" Binding="{Binding DomainName}" IsReadOnly="True" Width="105" CanUserResize="False"/>
<DataGridTextColumn Header="Druh" Binding="{Binding Relevance}" IsReadOnly="True" Width="65" CanUserResize="False"/>
<DataGridTextColumn Header="Kategorie" Binding="{Binding DomainName}" IsReadOnly="True" Width="65" CanUserResize="False"/>
<DataGridTextColumn Header="Popis" Binding="{Binding Content}" IsReadOnly="True" Width="200" CanUserResize="False"/>
<DataGridTemplateColumn MinWidth="100" Header="Poznámky" CanUserSort="True" CanUserResize="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="BtnShowNotes" IsEnabled="True" Click="BtnShowNotes_OnClick">Zobrazit</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>