在WPF中将按钮添加到DataGrid

时间:2018-08-09 10:55:41

标签: c# wpf datagrid

我想问一下如何在WPF中将Button添加到DataGrid中。

这是我在XAML中的DataGrid

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

这些是资源管理器中该按钮(TemplateColumn)的属性: enter image description here

如何添加一个Button列中的DataGrid


这是我当前使用WPF制作的应用程序

请注意,按钮显示为!(IsEnabled)。

enter image description here

这是相同的应用程序,但是使用WinForms制作的:

这里的按钮是可单击的,因此我可以通过单击它来调用任何动作。

enter image description here

谢谢大家!

1 个答案:

答案 0 :(得分:1)

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