我有一个带有几个记录的数据网格。网格非常简单,因为它只有四列。网格的最后两列分别是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));
    }
}


另外添加此Binding:

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


加一工作。

可以肯定的是,还有另一种聪明的方法可以做到这一点,但这在我头上。

10-06 05:13
查看更多