数据绑定,属性更改事件

时间:2016-07-12 17:10:38

标签: c# wpf xaml

我有一个包含3列的数据网格(文本框列,组合框列,复选框列),我想发布一个事件 当在组合框列中选择一个值时,将相应地更改复选框列的值。

(我不希望它按照代码编写的方式工作,我根本不知道该怎么做..)

例如在此代码中,当在组合框列中选择了低于90的等级(绑定到Grade属性)时,我想要复选框列的值(绑定到{ {1}}属性)更改为GoodStudent,当其高于90时,更改为false

如果可能的话没有额外的按钮。

感谢。

ViewModel类:

true

XAML:

 public class Student : INotifyPropertyChanged
{
    public ObservableCollection<int> Grades { get; set; }
    private string _Name;
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Name"));
        }
    }
    private int _Grade;
    public int Grade
    {
        get
        {
            return _Grade;
        }
        set
        {
            _Grade = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Grade"));
        }
    }
    private bool _GoodStudent;
    public bool GoodStudent
    {
        get
        {
            return _GoodStudent;
        }
        set
        {
            _GoodStudent = value;
            OnPropertyChanged(new PropertyChangedEventArgs("GoodStudent"));
        }
    }
    public Student(string name, int g)
    {
        Grades = new ObservableCollection<int> { 30, 40, 90, 100 };
        this.Name = name;
        this.Grade = g;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }
}
class ViewModel
{
    public ObservableCollection<Student> Students { get; set; }
    public ViewModel()
    {
        Students = new ObservableCollection<Student>
        {
            new Student("Dan",80),
            new Student("Micheal",90)
        };
    }
}

1 个答案:

答案 0 :(得分:0)

快速而肮脏的解决方案:

我强烈建议您将Grades移出Student并将其放入ViewModel中

<强>型号:

public class Student : INotifyPropertyChanged
    {
        public ObservableCollection<int> Grades { get; set; }
        private string _Name;
        public string Name {
            get {
                return _Name;
            }
            set {
                _Name = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Name"));
            }
        }
        private int _Grade;
        public int Grade {
            get {
                return _Grade;
            }
            set {
                _Grade = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Grade"));
                OnPropertyChanged(new PropertyChangedEventArgs("GoodStudent"));
            }
        }
        private bool _GoodStudent;
        public bool GoodStudent {
            get { return this.Grade >= 90; }
        }
        public Student(string name, int g)
        {
            Grades = new ObservableCollection<int> { 30, 40, 90, 100 };
            this.Name = name;
            this.Grade = g;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }
    }

<强>的Xaml:

<Grid>
        <DataGrid ItemsSource="{Binding Students}" AutoGenerateColumns="False"  Margin="69,50,47,66">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="Names"/>
                <DataGridComboBoxColumn SelectedValueBinding="{Binding Grade}" Header="Grades" Width="80">
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="ItemsSource" Value="{Binding Grades, Mode=TwoWay , UpdateSourceTrigger=PropertyChanged}" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
                <DataGridCheckBoxColumn Binding="{Binding GoodStudent , UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" Header="IsGood" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>