wpf中datagrid内的复选框 [英] Checkbox inside datagrid in wpf

查看:126
本文介绍了wpf中datagrid内的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



Hi all

<DataGrid AutoGenerateColumns="False" Height="141" HorizontalAlignment="Left" Margin="114,109,0,0" Name="DataGrid1" VerticalAlignment="Top" Width="353" ItemsSource="{Binding}" >
<DataGrid.Columns>
    <DataGridTextColumn Header="Alias" Binding="{Binding ItemAdAlias}" Width="50"/>
  <DataGridTextColumn Header="Min Stock" Binding="{Binding ItemAdMin}" Width="60"/>
 <DataGridTextColumn Header="Max Stock" Binding="{Binding ItemAdMax}"/>
 <DataGridTextColumn Header="Current Stock" Binding="{Binding ItemAdCurr}"/>
 <DataGridTextColumn Header="Color" Binding="{Binding ItemAdColor}"/>
 <DataGridTextColumn Header="Set" Binding="{Binding ItemAdSet}"/>
 <DataGridTextColumn Header="Remarks" Binding="{Binding ItemAdRemarks}"/>
</DataGrid.Columns>
                   
   </DataGrid>





这是我的数据网格我的应用程序。

现在我必须添加另一行,每行对应一个复选框,然后选中并取消选中保存并从中获取值数据库。



请告诉我怎么做..

或其他任何替代想法都会有所帮助。



谢谢你



This is my datagrid my application.
now i have to add another row with checkboxes corresponding to each column and on check and uncheck save and fetch the value from the database.

Please tell me how to do this..
or any other alternate idea will be helpful.

Thank you

推荐答案

你可以在里面使用CheckBox来使用 DataGridCheckBoxColumn DataGridTemplateColumn 。检查这些链接以了解如何做到这一点。

将复选框列添加到WPF数据网格并选择已选中的行 [ ^ ]

在WPF Datagrid DatagridTemplateColumnHeader 中添加复选框[ ^ ]
You can use the DataGridCheckBoxColumn or a DataGridTemplateColumn with a CheckBox inside. Check these links to get an idea how to do that.
adding the checkbox column in to WPF datagrid and select the checked rows [^]
Add checkbox in WPF Datagrid DatagridTemplateColumnHeader[^]


<wpf:DataGridTemplateColumn Width="SizeToHeader" IsReadOnly="True" Header="First Selection">
    <wpf:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Path=First}"  Checked="OnChecked" Margin="2,0,2,0" HorizontalAlignment="Center" VerticalAlignment="Stretch" />
        </DataTemplate>
    </wpf:DataGridTemplateColumn.CellTemplate>







你可以使用模板列在datagrid中添加复选框。




You can use template column to add checkbox in datagrid .


我去了非常简单..



I went with very simple..

<DataGridCheckBoxColumn Binding="{Binding IsChecked, Mode=TwoWay}" IsReadOnly="False">
                                <DataGridCheckBoxColumn.HeaderTemplate>
                                    <DataTemplate>
                                        <CheckBox Checked="FieldDataGridChecked" Unchecked="FieldDataGridUnchecked" Margin="2" />
                                    </DataTemplate>
                                </DataGridCheckBoxColumn.HeaderTemplate>
                            </DataGridCheckBoxColumn>





只需处理已检查和未检查的事件,并将已检查应用于模型。





Just handle the checked and unchecked events, and apply the "checked" to the model.

private void FieldDataGridChecked(object sender, RoutedEventArgs e)
        {
            foreach (FieldViewModel model in _fields)
            {
                model.IsChecked = true;
            }
        }

        private void FieldDataGridUnchecked(object sender, RoutedEventArgs e)
        {
            foreach (FieldViewModel model in _fields)
            {
                model.IsChecked = false;
            }
        }


这篇关于wpf中datagrid内的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆