组合框在其项目中不显示任何内容,但是当我选择任何项目时,我会得到一些数据 [英] Combobox does not show anything in its items but when I select any items I get some data

查看:26
本文介绍了组合框在其项目中不显示任何内容,但是当我选择任何项目时,我会得到一些数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在组合框上工作了超过 2 天,但没有找到解决方案.

I am working on a combobox for more than 2 days but did not find the solution.

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

In one of my question a user named JnJnBoo tried to answer my question and I got some knowledge and code from there.

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

I am trying to display some data in a combobox in multiple columns using MVVM pattern. I am using entity framework and SQL Server database.

代码如下:

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>

我尝试了很多东西,但总是不成功.

I tried many things but always unsuccessful.

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

My Combobox is not displaying any data in its items but when I select any Item in the combobox then I get some data in the selectedGroup property. The data is namespace.classname

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

So I think I need to override the Tostring Method of GroupAndCorrespondingEffect class. But It has got two properties. In which format the databinding in XAML expects the data that is not known to me. So, how to override the tostring method? Or might be I am making some sort of mistake in my code?

推荐答案

您的 GroupAndCorrespondingEffect 应如下所示

Your GroupAndCorrespondingEffect should look like the following

 public class GroupAndCorrespondingEffect
 {
     public string GroupName;
     {
        get;

        set;
     }
     public string CorrespondingEffect;
     {
        get;

        set;
     }
 }

在你的 XAML 中

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

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

The property name is wrong it contains additional s

应该是这样

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

这篇关于组合框在其项目中不显示任何内容,但是当我选择任何项目时,我会得到一些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆