简体   繁体   English

从wpf数据网格中的复选框中选择值

[英]select the value from a checkbox in wpf data grid

I am trying to select the value from the datagrid checkbox (whether it is checked or unchecked) and second column(UserName). 我试图从datagrid复选框(无论是选中还是未选中)和第二列(UserName)中选择值。 Here is the datagrid code. 这是datagrid代码。

     <DataGrid AutoGenerateColumns="False"  Name="enableDataGrid" ItemsSource="{Binding}" Margin="0,0,0,81">
     <DataGrid.Columns>
     <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>                                           
             <CheckBox HorizontalAlignment="Center"/>                                      
          </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>
     <DataGridTextColumn Header="User Name" Binding="{Binding Path=uName}"/>
     </DataGrid.Column>
     </DataGrid>

How should i achieve it ?Please suggest. 我应该如何实现它?请建议。

改为使用DataGridCheckBoxColumn

<DataGridCheckBoxColumn Binding="{Binding IsChecked}" />

Bind the checkbox to a property in your itemsource then you can get the selected row from the datagrid 将复选框绑定到itemsource中的属性,然后您可以从datagrid获取所选行

<CheckBox HorizontalAlignment="Center" IsChecked="{Binding myBool}"/>  

MyDatagridItem item = MyDatagrid.SelectedItem as MyDatagridItem

if(item.myBool == true){
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM