Skip to main content
 首页 » 编程设计

c#之Datagrid-复选框选择和取消选择

2024年02月20日10Renyi-Fan

我有一个包含几条记录的数据网格。网格非常简单,因为它只有四列。网格的最后两列分别是 Option1 和 Option2。现在我在这里遇到一个问题,如果是从 Option1 列中选择行的复选框,然后如果我要在同一行上选择 Option2,那么应该取消选择从该行中选择的 Option1。我也尝试使用单选按钮,但它不起作用,因为它选中或取消选中了整行。我希望操作应该发生在同一行。

谢谢

代码-

<Window x:Class="Convertor.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    Title="MainWindow" Height="350" Width="525"> 
 
<Grid> 
    <DataGrid x:Name="dgEmp" CanUserAddRows="False" AutoGenerateColumns="False"> 
        <DataGrid.Columns> 
            <DataGridTextColumn Header="Id" Binding="{Binding Path=Id}" Width="*"></DataGridTextColumn> 
            <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="*"></DataGridTextColumn> 
            <DataGridTemplateColumn Header="Option1" Width="*"> 
                <DataGridTemplateColumn.CellTemplate> 
                    <DataTemplate> 
                        <CheckBox x:Name="ch1"></CheckBox> 
                    </DataTemplate> 
                </DataGridTemplateColumn.CellTemplate> 
            </DataGridTemplateColumn> 
            <DataGridTemplateColumn Header="Option2" Width="*"> 
                <DataGridTemplateColumn.CellTemplate> 
                    <DataTemplate> 
                        <CheckBox x:Name="ch2" ></CheckBox> 
                    </DataTemplate> 
                </DataGridTemplateColumn.CellTemplate> 
            </DataGridTemplateColumn> 
        </DataGrid.Columns> 
    </DataGrid> 
 
</Grid> 

 private ObservableCollection<Emp> _empList; 
    public MainWindow() 
    { 
        InitializeComponent(); 
        BindEmpDetails(); 
    } 
 
    private void BindEmpDetails() 
    { 
        _empList = new ObservableCollection<Emp>() 
        { 
            new Emp(){Id=1,Name="XYZ"}, 
            new Emp(){Id=1,Name="ABC"}, 
        }; 
        dgEmp.ItemsSource = _empList; 
    } 
} 
 
public class Emp 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public bool Working { get; set; } 
    public bool Retired { get; set; } 
} 

请您参考如下方法:

我会做什么(Emp 应该实现 INotifyProperyChanged):

 public class Emp : INotifyPropertyChanged 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public bool Working 
    { 
        get { return working_; } 
        set 
        { 
            if (working_ != value) 
            { 
                working_ = value; 
                retired_ = !working_; 
                OnPropertyChanged("Retired"); 
                OnPropertyChanged("Working"); 
            } 
 
        } 
    } 
    public bool Retired 
    { 
        get { return retired_; } 
        set 
        { 
            if (retired_ != value) 
            { 
                retired_ = value; 
                working_ = !retired_; 
                OnPropertyChanged("Retired"); 
                OnPropertyChanged("Working"); 
            } 
 
        } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
    private bool retired_; 
    private bool working_; 
    public void OnPropertyChanged(string PropertyName) { 
        if (PropertyChanged != null) 
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); 
    } 
} 

加上这个绑定(bind):

<CheckBox x:Name="ch1" Checked="{Binding Retired}"></CheckBox> 

加一个用于工作。

很确定还有另一种聪明的方法,但这个在我的脑海中。