0

我在组合框上工作超过 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 属性中获得一些数据。数据是 namespace.classname

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

4

3 回答 3

1

您的 GroupAndCorrespondingEffect 应如下所示

 public class GroupAndCorrespondingEffect
 {
     public string GroupName;
     {
        get;

        set;
     }
     public string CorrespondingEffect;
     {
        get;

        set;
     }
 }

在你的 XAML 中

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

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

所以应该是

<TextBlock Text="{Binding Path=CorrespondingEffect}"/>
于 2013-10-26T02:52:36.493 回答
0
public class GroupAndCorrespondingEffect
{
    public string GroupName;
    public string CorrespondingEffect;
}

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

于 2013-10-26T02:36:51.793 回答
0

并在您的视图模型中更改属性 SelectedGroup 的类型,如下所示

    private GroupAndCorrespondingEffect _selectedGroup;
    public GroupAndCorrespondingEffect SelectedGroup
    {
        get
        {
            return _selectedGroup;
        }
        set
        {
            _selectedGroup = value;
            OnPropertyChanged("SelectedGroup");
        }
    }
于 2013-10-26T03:01:16.260 回答