目标是将启用的
Button
插入到 DataGrid
的列中。
在以下 XAML 代码片段中,将
Button
插入到 DataGrid
中,并将 IsEnabled
属性设置为 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>
但是,执行应用程序时,该按钮被禁用。
是什么导致了这个问题?
您的
RowStyle
将整行的 IsEnabled
属性设置为 false
。删除此一项,Button
应启用:
<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>