简体   繁体   中英

Datagrid-Checkbox select and unselect

I have a data grid with couple of records.well the grid is very simple as it is having only four column. The last two column of the grid are Option1 and Option2 respectively. Now i have an issue here if is selects checkbox of row from Option1 column and then if i am going to select Option2 on the very same row then the selected Option1 from that row should be unselect. I also tried with the radio button but it is not working as it checked or unchecked the whole rows. what i wants that the operation should be happen on the same row.

Thanks

code-

<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; }
}

What I would do (Emp should implement 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));
    }
}

Plus add this Binding :

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

Plus one for the working.

Pretty sure there is another clever way of doing it, but this on top of my head.

This seems like it would be best with radio buttons, as you tried before. If you give two radio buttons the same GroupName , they should be mutually exclusive regardless of their location in the window. This way, the user would only be able to choose "Working" or "Retired."

<RadioButton GroupName="IsWorking" Content="Working" />
<RadioButton GroupName="IsWorking" Content="Retired" />

http://msdn.microsoft.com/en-us/library/system.windows.controls.radiobutton.groupname.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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