Combobox does not show anything in its items but w

2019-08-20 21:59发布

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

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

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.

Here is the code :

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;
    }
}

And the 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.

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

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?

标签: c# wpf xaml mvvm
3条回答
我命由我不由天
2楼-- · 2019-08-20 22:35

Your GroupAndCorrespondingEffect should look like the following

 public class GroupAndCorrespondingEffect
 {
     public string GroupName;
     {
        get;

        set;
     }
     public string CorrespondingEffect;
     {
        get;

        set;
     }
 }

And in you XAML

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

The property name is wrong it contains additional s

so it should be

<TextBlock Text="{Binding Path=CorrespondingEffect}"/>
查看更多
可以哭但决不认输i
3楼-- · 2019-08-20 22:36

And in your View model change the type of the property SelectedGroup like below

    private GroupAndCorrespondingEffect _selectedGroup;
    public GroupAndCorrespondingEffect SelectedGroup
    {
        get
        {
            return _selectedGroup;
        }
        set
        {
            _selectedGroup = value;
            OnPropertyChanged("SelectedGroup");
        }
    }
查看更多
Juvenile、少年°
4楼-- · 2019-08-20 22:44
public class GroupAndCorrespondingEffect
{
    public string GroupName;
    public string CorrespondingEffect;
}

Make the public variables GroupName and CorrespondingEffect as properties

查看更多
登录 后发表回答