Combobox在其项目中不显示任何内容,但是当我选择任何项目时,我会得到一些数据

本文关键字:项目 选择 任何 数据 显示 任何内 Combobox | 更新日期: 2023-09-27 18:36:53

>我在组合盒上工作了 2 天多,但没有找到解决方案。

在我的一个问题中,一个名为JnJnBoo的用户试图回答我的问题,我从那里获得了一些知识和代码。

我正在尝试使用 MVVM 模式在多列的组合框中显示一些数据。我正在使用实体框架和SQL Server数据库。

这是代码:

namespace ERP_Lite_Trial.ViewModels
{
    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();
                EffectName = (from e in db.Type_Effect
                            select e.Name).ToList();
            }
        }
        private List<GroupAndCorrespondingEffect> _groupsAndCorrespondingEffects;
        public List<GroupAndCorrespondingEffect> GroupsAndCorrespondingEffects
        {
            get
            {
                return _groupsAndCorrespondingEffects;
            }
            set
            {
                _groupsAndCorrespondingEffects = value;
                OnPropertyChanged("GroupsAndCorrespondingEffects");
            }
        }
        private string _selectedGroup;
        public string SelectedGroup
        {
            get
            {
                return _selectedGroup;
            }
            set
            {
                _selectedGroup = value;
                OnPropertyChanged("SelectedGroup");
            }
        }
        private List<string> _effectName;
        public List<string> EffectName
        {
            get
            {
                return _effectName;
            }
            set
            {
                _effectName = value;
                OnPropertyChanged("EffectName");
            }
        }
        public void OnPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class GroupAndCorrespondingEffect
    {
        public string GroupName;
        public string CorrespondingEffect;
    }
}

和 XAML :

<ComboBox x:Name="cbUnder" ItemsSource="{Binding Path=GroupsAndCorrespondingEffects}"
          IsEditable="True" SelectedItem="{Binding Path=SelectedGroup, Mode=TwoWay}" 
          TextSearch.TextPath="GroupName" Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="3">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Path=GroupName}"/>
                <TextBlock Text="{Binding Path=CorrespondingEffects}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

我尝试了很多事情,但总是不成功。

我的组合框没有在其项目中显示任何数据,但是当我在组合框中选择任何项目时,我会在 selectedGroup 属性中获得一些数据。数据是命名空间.类名

所以我认为我需要覆盖 GroupAndCorrespondingEffect 类的 Tostring 方法。但它有两个属性。XAML 中的数据绑定以哪种格式需要我不知道的数据。那么,如何覆盖tostring方法呢?或者可能是我在代码中犯了某种错误?

Combobox在其项目中不显示任何内容,但是当我选择任何项目时,我会得到一些数据

你的组和相应的效果应该如下所示

 public class GroupAndCorrespondingEffect
 {
     public string GroupName;
     {
        get;
        set;
     }
     public string CorrespondingEffect;
     {
        get;
        set;
     }
 }

在你 XAML 中

<TextBlock Text="{Binding Path=CorrespondingEffects}"/>

属性名称错误,它包含额外的

所以应该是

<TextBlock Text="{Binding Path=CorrespondingEffect}"/>
public class GroupAndCorrespondingEffect
{
    public string GroupName;
    public string CorrespondingEffect;
}

将公共变量 GroupName 和 CorrespondingEffect 作为属性

并在您的视图模型中更改属性的类型 选定组

如下所示
    private GroupAndCorrespondingEffect _selectedGroup;
    public GroupAndCorrespondingEffect SelectedGroup
    {
        get
        {
            return _selectedGroup;
        }
        set
        {
            _selectedGroup = value;
            OnPropertyChanged("SelectedGroup");
        }
    }