在 WPF 中的 DataGrid 中添加按钮

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


这是我DataGrid在 XAML 中的:


<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) 的属性:

http://img1.mukewang.com/6199f9a2000162e706080754.jpg

如何添加Button将在此列中的DataGrid


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

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

http://img.mukewang.com/6199f9ad00012e5106330303.jpg

这是同一个应用程序,但使用 WinForms 制作:

这里的按钮是可点击的,所以我可以通过点击它来调用任何操作。

http://img4.mukewang.com/6199f9ba0001ad5f07040147.jpg


慕容708150
浏览 186回答 1
1回答

手掌心

您RowStyle将IsEnabled整行的属性设置为false。删除这个,Button应该启用:<DataGrid Name="DgData"&nbsp; HorizontalAlignment="Left" Height="306" Margin="10,119,0,0" VerticalAlignment="Top" Width="637" Background="White">&nbsp; &nbsp; <DataGrid.Columns>&nbsp; &nbsp; &nbsp; &nbsp; <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Id" IsReadOnly="True" Visibility="Hidden"/>&nbsp; &nbsp; &nbsp; &nbsp; <DataGridTextColumn Header="Datum" Binding="{Binding Date}" IsReadOnly="True" Width="100" CanUserResize="False"/>&nbsp; &nbsp; &nbsp; &nbsp; <DataGridTextColumn Header="Doménové jméno" Binding="{Binding DomainName}" IsReadOnly="True" Width="105" CanUserResize="False"/>&nbsp; &nbsp; &nbsp; &nbsp; <DataGridTextColumn Header="Druh" Binding="{Binding Relevance}" IsReadOnly="True" Width="65" CanUserResize="False"/>&nbsp; &nbsp; &nbsp; &nbsp; <DataGridTextColumn Header="Kategorie" Binding="{Binding DomainName}" IsReadOnly="True" Width="65" CanUserResize="False"/>&nbsp; &nbsp; &nbsp; &nbsp; <DataGridTextColumn Header="Popis" Binding="{Binding Content}" IsReadOnly="True" Width="200" CanUserResize="False"/>&nbsp; &nbsp; &nbsp; &nbsp; <DataGridTemplateColumn MinWidth="100" Header="Poznámky" CanUserSort="True" CanUserResize="False">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button Name="BtnShowNotes" IsEnabled="True" Click="BtnShowNotes_OnClick">Zobrazit</Button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; </DataGridTemplateColumn>&nbsp; &nbsp; </DataGrid.Columns></DataGrid>
打开App,查看更多内容
随时随地看视频慕课网APP