簡體   English   中英

Datagrid-Checkbox選擇和取消選擇

[英]Datagrid-Checkbox select and unselect

我有一個帶有幾個記錄的數據網格。網格非常簡單,因為它只有四列。 網格的最后兩列分別是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));
    }
}

另外添加此綁定:

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

加一個工作。

很確定還有另一個聰明的方法,但這是我的頭腦。

這似乎是最好的單選按鈕,就像你之前嘗試過的那樣。 如果給兩個單選按鈕指定相同的GroupName ,則它們應該是互斥的,無論它們在窗口中的位置如何。 這樣,用戶只能選擇“工作”或“退休”。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM