C# 将公共属性写入MVVM中的类文件

C# 将公共属性写入MVVM中的类文件,c#,wpf,silverlight,mvvm,combobox,C#,Wpf,Silverlight,Mvvm,Combobox,我有属性CaretIndex和InputValueim我的ViewModel,用于突出显示组合框项目,这些项目与组合框的部分可编辑文本框中的文本相匹配。 所以,我希望他们在一个单独的文件。因此,我不必在每次声明新视图时都键入它们。但这里的问题是,这些CaretIndex和InputValue属性的setter部分依赖于另一个类的另一个名为IsHighlighted的属性,并且该类可能会因每个新的数据集合而改变 我有一个ViewModel,如下所示: public class GroupsView

我有属性
CaretIndex
InputValue
im我的
ViewModel
,用于
突出显示组合框项目,这些项目与组合框
的部分可编辑文本框中的文本相匹配。 所以,我希望他们在一个单独的文件。因此,我不必在每次声明新视图时都键入它们。但这里的问题是,这些
CaretIndex
InputValue
属性的setter部分依赖于另一个类的另一个名为
IsHighlighted
的属性,并且该类可能会因每个新的数据集合而改变

我有一个ViewModel,如下所示:

public class GroupsViewModel : INotifyPropertyChanged
{
    public GroupsViewModel()
    {
        using (DBEntities db = new DBEntities())
        {
            GroupsAndCorrespondingEffects = (from g in db.Groups
                                             select new GroupAndCorrespondingEffect
                                                        {
                                                            GroupName = g.Name,
                                                            CorrespondingEffect = g.Type_Effect.Name
                                                        }
                                            ).ToList();

            GroupsAndCorrespondingEffects.Add
                                            (
                                                new GroupAndCorrespondingEffect
                                                        {
                                                            GroupName = " Primary",
                                                            CorrespondingEffect = ""
                                                        }
                                            );

            GroupsAndCorrespondingEffects = GroupsAndCorrespondingEffects.OrderBy(g => g.GroupName).ToList();

            Items = (from e in db.Type_Effect
                     select e.Name).ToList();
        }
    }

    public static GroupsViewModel CurrentInstance { get { return Instance; } }

    private List<GroupAndCorrespondingEffect> _groupsAndCorrespondingEffects;
    public List<GroupAndCorrespondingEffect> GroupsAndCorrespondingEffects
    {
        get
        {
            return _groupsAndCorrespondingEffects;
        }
        set
        {
            _groupsAndCorrespondingEffects = value;
            OnPropertyChanged("GroupsAndCorrespondingEffects");
        }
    }

    private int _caretIndex;
    public int CaretIndex
    {
        get { return _caretIndex; }
        set
        {
            _caretIndex = value;
            OnPropertyChanged("CaretIndex");

            for (int i = 0; i < GroupsAndCorrespondingEffects.Count; i++)
            {

                string WordToSearch = InputValue;

                if (_caretIndex != 0 && _caretIndex > 0)
                {
                    WordToSearch = InputValue.Substring(0, _caretIndex);
                }

                if (WordToSearch != null)
                {
                    GroupsAndCorrespondingEffects[i].IsHighlighted = GroupsAndCorrespondingEffects[i].GroupName.StartsWith(WordToSearch);
                }
            }
        }
    }

    private string _inputValue;
    public string InputValue
    {
        get { return _inputValue; }
        set
        {
            _inputValue = value;
            OnPropertyChanged("GroupsAndCorrespondingEffects");

            for (int i = 0; i < GroupsAndCorrespondingEffects.Count; i++)
            {

                string WordToSearch = _inputValue;

                if (_caretIndex != 0 && _caretIndex > 0 && _caretIndex < _inputValue.Length)
                {
                    WordToSearch = _inputValue.Substring(0, _caretIndex);
                }

                GroupsAndCorrespondingEffects[i].IsHighlighted = GroupsAndCorrespondingEffects[i].GroupName.StartsWith(WordToSearch);

            }
        }
    }

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

如果我正确理解了您的问题,您要做的是将公共位(如
CaretIndex
)和
InputValue
移动到抽象基类

请参阅下面的示例代码以了解我的意思:

基类

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private int _caretIndex;

    public int CaretIndex
    {
        get { return _caretIndex; }
        set
        {
            _caretIndex = value;
            OnPropertyChanged("CaretIndex");
            OnCaretIndexChanged();
        }
    }

    private string _inputValue;
    public string InputValue
    {
        get { return _inputValue; }
        set
        {
            _inputValue = value;
            OnPropertyChanged("InputValue");
            OnInputValueChanged();
        }
    }

    protected abstract void OnCaretIndexChanged();

    protected abstract void OnInputValueChanged();
}
如果您检查设置器中的CaretIndex和InputValue,则它们分别执行
OnCaretIndexChanged
OnInputValueChanged
抽象方法,您将在派生类中实现这些方法

现在,您的
GroupViewModel
将继承
ViewModelBase
并实现这两个抽象方法

public class GroupsViewModel : ViewModelBase
{
    public GroupsViewModel()
    {
        using (DBEntities db = new DBEntities())
        {
            GroupsAndCorrespondingEffects = (from g in db.Groups
                                             select new GroupAndCorrespondingEffect
                                             {
                                                 GroupName = g.Name,
                                                 CorrespondingEffect = g.Type_Effect.Name
                                             }
                                            ).ToList();

            GroupsAndCorrespondingEffects.Add
                                            (
                                                new GroupAndCorrespondingEffect
                                                {
                                                    GroupName = " Primary",
                                                    CorrespondingEffect = ""
                                                }
                                            );

            GroupsAndCorrespondingEffects = GroupsAndCorrespondingEffects.OrderBy(g => g.GroupName).ToList();

            Items = (from e in db.Type_Effect
                     select e.Name).ToList();
        }
    }

    public static GroupsViewModel CurrentInstance { get { return Instance; } }

    private List<GroupAndCorrespondingEffect> _groupsAndCorrespondingEffects;
    public List<GroupAndCorrespondingEffect> GroupsAndCorrespondingEffects
    {
        get
        {
            return _groupsAndCorrespondingEffects;
        }
        set
        {
            _groupsAndCorrespondingEffects = value;
            OnPropertyChanged("GroupsAndCorrespondingEffects");
        }
    }

    protected override void OnCaretIndexChanged()
    {
        for (int i = 0; i < GroupsAndCorrespondingEffects.Count; i++)
        {
            string wordToSearch = InputValue;

            if (CaretIndex != 0 && CaretIndex > 0)
            {
                wordToSearch = InputValue.Substring(0, CaretIndex);
            }

            if (wordToSearch != null)
            {
                GroupsAndCorrespondingEffects[i].IsHighlighted = GroupsAndCorrespondingEffects[i].GroupName.StartsWith(wordToSearch);
            }
        }
    }

    protected override void OnInputValueChanged()
    {
        OnPropertyChanged("GroupsAndCorrespondingEffects");

        for (int i = 0; i < GroupsAndCorrespondingEffects.Count; i++)
        {

            string wordToSearch = InputValue;

            if (CaretIndex != 0 && CaretIndex > 0 && CaretIndex < InputValue.Length)
            {
                wordToSearch = InputValue.Substring(0, CaretIndex);
            }

            GroupsAndCorrespondingEffects[i].IsHighlighted = GroupsAndCorrespondingEffects[i].GroupName.StartsWith(wordToSearch);

        }
    }
}
公共类组viewmodel:ViewModelBase
{
公共组viewmodel()
{
使用(DBEntities db=new DBEntities())
{
Groups和correspondingeffects=(来自db.Groups中的g
选择新组和相应的效果
{
GroupName=g.Name,
对应的效果=g.Type_Effect.Name
}
).ToList();
组和相应的效果。添加
(
新的组和相应的效果
{
GroupName=“Primary”,
对应的效果=“”
}
);
GroupsAndCorrespondingEffects=GroupsAndCorrespondingEffects.OrderBy(g=>g.GroupName.ToList();
项目=(从db.Type_效应中的e开始)
选择e.Name).ToList();
}
}
公共静态组viewmodel CurrentInstance{get{return Instance;}}
私有列表组和相应的效果;
公共列表组和相应的效果
{
得到
{
返回组和相应的效果;
}
设置
{
_组和相应的效果=值;
关于财产变更(“集团和相应影响”);
}
}
受保护的覆盖无效OnCaretIndexChanged()
{
对于(int i=0;i0)
{
wordToSearch=InputValue.Substring(0,CaretIndex);
}
if(wordToSearch!=null)
{
GroupsAndCorrespondingEffects[i]。IsHighlighted=GroupsAndCorrespondingEffects[i]。GroupName.StartsWith(wordToSearch);
}
}
}
受保护的覆盖无效OnInputValueChanged()
{
关于财产变更(“集团和相应影响”);
对于(int i=0;i0&&CaretIndex
如果要从类
组和相应的效果中抽象出
IsHighlighted
属性,可以采用类似的方法

希望这对你有所帮助或给你一些想法

更新

添加类图


您好,先生,很高兴在这里再次见到您。我跟着你的时候会有麻烦。在我的视图中有一个名为cbUnder的两列组合框,名为Groups.xaml,因此,为了将它的两列值组合起来,我创建了名为groupandcorrespondingfect的类。现在为了突出显示项目,我在这个类中声明了IsHighlighted属性。现在,如果我为IsHighlighted属性创建另一个抽象类,那么它将在这个场景中工作。考虑另一个场景,在我的视图中还有另一个组合框,它只有1列。在第二条评论中继续…………继续…………在那里我将不会有另一个课程。但我还是想为那个组合框点亮灯光。现在,如果我有另一个IsHighlighted抽象类,那么我的Viewmodel也需要从该抽象类继承。但据我所知,一个类不能从多个基类派生。现在您会说我应该在ViewModelBase类中创建IsHighlighted属性,正如您在回答中所建议的那样。但问题是,我有另一个没有任何组合框的视图。所以,再一次,我不需要任何IsHighlighted属性。@Khushi提到,如果你想抽象出IsHighlighted,你可以做类似的事情。然而,从你的评论中,我看到这不是你想要的。我不会要求您将IsHighlighted移动到ViewModelBase,因为它不属于那里。IsHighlighted仅在子视图模型中相关。您是否也有许多具有IsHighlighed属性重复代码的ViewModels?是的,我有大约60%的ViewModels
public class GroupsViewModel : ViewModelBase
{
    public GroupsViewModel()
    {
        using (DBEntities db = new DBEntities())
        {
            GroupsAndCorrespondingEffects = (from g in db.Groups
                                             select new GroupAndCorrespondingEffect
                                             {
                                                 GroupName = g.Name,
                                                 CorrespondingEffect = g.Type_Effect.Name
                                             }
                                            ).ToList();

            GroupsAndCorrespondingEffects.Add
                                            (
                                                new GroupAndCorrespondingEffect
                                                {
                                                    GroupName = " Primary",
                                                    CorrespondingEffect = ""
                                                }
                                            );

            GroupsAndCorrespondingEffects = GroupsAndCorrespondingEffects.OrderBy(g => g.GroupName).ToList();

            Items = (from e in db.Type_Effect
                     select e.Name).ToList();
        }
    }

    public static GroupsViewModel CurrentInstance { get { return Instance; } }

    private List<GroupAndCorrespondingEffect> _groupsAndCorrespondingEffects;
    public List<GroupAndCorrespondingEffect> GroupsAndCorrespondingEffects
    {
        get
        {
            return _groupsAndCorrespondingEffects;
        }
        set
        {
            _groupsAndCorrespondingEffects = value;
            OnPropertyChanged("GroupsAndCorrespondingEffects");
        }
    }

    protected override void OnCaretIndexChanged()
    {
        for (int i = 0; i < GroupsAndCorrespondingEffects.Count; i++)
        {
            string wordToSearch = InputValue;

            if (CaretIndex != 0 && CaretIndex > 0)
            {
                wordToSearch = InputValue.Substring(0, CaretIndex);
            }

            if (wordToSearch != null)
            {
                GroupsAndCorrespondingEffects[i].IsHighlighted = GroupsAndCorrespondingEffects[i].GroupName.StartsWith(wordToSearch);
            }
        }
    }

    protected override void OnInputValueChanged()
    {
        OnPropertyChanged("GroupsAndCorrespondingEffects");

        for (int i = 0; i < GroupsAndCorrespondingEffects.Count; i++)
        {

            string wordToSearch = InputValue;

            if (CaretIndex != 0 && CaretIndex > 0 && CaretIndex < InputValue.Length)
            {
                wordToSearch = InputValue.Substring(0, CaretIndex);
            }

            GroupsAndCorrespondingEffects[i].IsHighlighted = GroupsAndCorrespondingEffects[i].GroupName.StartsWith(wordToSearch);

        }
    }
}