wpf 检测自定义类的属性何时更改

ffscu2ro  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(180)

我有两门课:ParentClassChildClass,两者都用作具有类似层次结构UserControl数据上下文
我想让父类在它的自定义类属性的某个变量发生更改时得到通知。
第一个
我想要的是在Score发生变化时,执行父类的Child属性的set部分,并更新Value
我该怎么做?
编辑:程式码
属性更改通知

internal class PropertyChangedNotify : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    protected virtual void OnPropertyChanged(string? property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
}

熟练程度_计数器- xaml

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <TextBlock Text=""/>

    <Viewbox Grid.Column="1">
        <ComboBox ItemsSource="{Binding ProficiencyList}" SelectedItem="{Binding Proficiency}" Margin="1">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Viewbox>
                        <TextBlock Text="{Binding}"/>
                    </Viewbox>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Viewbox>
</Grid>

熟练度_计数器_模型- aka子类

internal class Proficiency_Counter_Model: PropertyChangedNotify
{
    #region private
    private int _proficiencyScore;
    private List<char> _proficiencyList;
    private char _proficiency;
    private int _level;
    #endregion
    #region public
    public int ProficiencyScore
    {
        get { return _proficiencyScore; }
        set
        {
            _proficiencyScore = value;
            OnPropertyChanged(nameof(ProficiencyScore));
        }
    }
    public List<char> ProficiencyList
    {
        get
        {
            if (_proficiencyList == null)
            {
                ProficiencyList = new List<char>(); 
                ProficiencyList.Add('U');
                ProficiencyList.Add('T');
                ProficiencyList.Add('E');
                ProficiencyList.Add('M');
                ProficiencyList.Add('L');
                Proficiency = 'U';
            }
            return _proficiencyList;
        }
        set
        {
            _proficiencyList = value;
            OnPropertyChanged(nameof(ProficiencyList));
        }
    }
    public char Proficiency
    {
        get { return _proficiency; }
        set
        {
            _proficiency = value;
            if (Proficiency == 'U')
            {
                ProficiencyScore = 0;
            }
            else if (Proficiency == 'T')
            {
                ProficiencyScore = 2 + _level;
            }
            else if (Proficiency == 'E')
            {
                ProficiencyScore = 4 + _level;
            }
            else if (Proficiency == 'M')
            {
                ProficiencyScore = 6 + _level;
            }
            else if (Proficiency == 'L')
            {
                ProficiencyScore = 8 + _level;
            }
            OnPropertyChanged(nameof(Proficiency));
        }
    }
    public int Level
    {
        get { return _level; }
        set
        {
            _level = value;
            Proficiency = _proficiency;
            OnPropertyChanged(nameof(Level));
        }
    }
    #endregion

}

常规技能计数器模型-也称为父类

internal class General_Skill_Counter_Model: PropertyChangedNotify
{
    #region private
    private string _skillName;
    private int _abilityMod;
    private Proficiency_Counter_Model _proficiency;
    private int _proficiencyMod;
    private int _itemMod;
    #endregion
    #region public
    public string SkillName
    {
        get
        {
            if (_skillName == null)
                SkillName = "Lorem Impsum";
            return _skillName;
        }
        set
        {
            _skillName = value;
            OnPropertyChanged(nameof(SkillName));
        }
    }
    public int SkillScore
    {
        get
        {
            return (AbilityMod + Proficiency.ProficiencyScore + ItemMod);
        }
    }
    public int AbilityMod
    {
        get { return _abilityMod; }
        set
        {
            _abilityMod = value;
            OnPropertyChanged(nameof(AbilityMod));
            OnPropertyChanged(nameof(SkillScore));
        }
    }
    public Proficiency_Counter_Model Proficiency
    {
        get
        {
            if (_proficiency == null)
            {
                _proficiency = new Proficiency_Counter_Model();
            }
            return _proficiency;
        }
        set
        {
            _proficiency = value;
            ProficiencyMod = _proficiency.ProficiencyScore;
            OnPropertyChanged(nameof(Proficiency));
        }
    }
    public int ProficiencyMod
    {
        get { return _proficiencyMod; }
        set
        {
            _proficiencyMod = value;
            OnPropertyChanged(nameof(ProficiencyMod));
            OnPropertyChanged(nameof(SkillScore));
        }
    }
    public int ItemMod
    {
        get { return _itemMod; }
        set
        {
            _itemMod = value;
            OnPropertyChanged(nameof(ItemMod));
            OnPropertyChanged(nameof(SkillScore));
        }
    }
    #endregion
}

Proficiency_Counter_Model被设置为Proficiency_Counter的DataContext,对于Genereal_Skill_Counter也是如此,我相信这与此无关。

doinxwow

doinxwow1#

好吧,我想通了,我没有通知家长,而是让孩子成为家长的一部分。
可悲的是,这只适用于单一类-它不会工作,如果我有2个不同的孩子同一个类。
代码如下:

internal class Proficiency_Counter_Model: PropertyChangedNotify
{
    #region private
    protected int _proficiencyScore;
    private List<char> _proficiencyList;
    private char _proficiency;
    protected int _level;
    #endregion
    #region public
    public virtual int ProficiencyScore
    {
        get { return _proficiencyScore; }
        set
        {
            _proficiencyScore = value;
            OnPropertyChanged(nameof(ProficiencyScore));
        }
    }
    public List<char> ProficiencyList
    {
        get
        {
            if (_proficiencyList == null)
            {
                ProficiencyList = new List<char>(); 
                ProficiencyList.Add('U');
                ProficiencyList.Add('T');
                ProficiencyList.Add('E');
                ProficiencyList.Add('M');
                ProficiencyList.Add('L');
                Proficiency = 'U';
            }
            return _proficiencyList;
        }
        set
        {
            _proficiencyList = value;
            OnPropertyChanged(nameof(ProficiencyList));
        }
    }
    public char Proficiency
    {
        get { return _proficiency; }
        set
        {
            _proficiency = value;
            if (Proficiency == 'U')
            {
                ProficiencyScore = 0;
            }
            else if (Proficiency == 'T')
            {
                ProficiencyScore = 2 + _level;
            }
            else if (Proficiency == 'E')
            {
                ProficiencyScore = 4 + _level;
            }
            else if (Proficiency == 'M')
            {
                ProficiencyScore = 6 + _level;
            }
            else if (Proficiency == 'L')
            {
                ProficiencyScore = 8 + _level;
            }
            OnPropertyChanged(nameof(Proficiency));
        }
    }
    public int Level
    {
        get { return _level; }
        set
        {
            _level = value;
            Proficiency = _proficiency;
            OnPropertyChanged(nameof(Level));
        }
    }
    #endregion

}

我将ProficiencyScore属性设为虚拟,以便覆写它。

internal class General_Skill_Counter_Model : Proficiency_Counter_Model
{
    #region private
    private string _skillName;
    private int _skillScore;
    private int _abilityMod;
    private int _itemMod;
    #endregion
    #region public
    public string SkillName
    {
        get
        {
            if (_skillName == null)
                SkillName = "Lorem Impsum";
            return _skillName;
        }
        set
        {
            _skillName = value;
            OnPropertyChanged(nameof(SkillName));
        }
    }
    public int SkillScore
    {
        get { return _skillScore; }
        set
        {
            _skillScore = value;
            OnPropertyChanged(nameof(SkillScore));
        }
    }
    public int AbilityMod
    {
        get { return _abilityMod; }
        set
        {
            _abilityMod = value;
            OnPropertyChanged(nameof(AbilityMod));
        }
    }
    public int ItemMod
    {
        get { return _itemMod; }
        set
        {
            _itemMod = value;
            OnPropertyChanged(nameof(ItemMod));
        }
    }
    #endregion

    #region override
    public override int ProficiencyScore
    {
        get { return _proficiencyScore; }
        set
        {
            SkillScore = _skillScore + value - _proficiencyScore;
            _proficiencyScore = value;
            OnPropertyChanged(nameof(ProficiencyScore));
        }
    }
    #endregion
}

相关问题