簡體   English   中英

組合框未顯示項目

[英]Combobox not showing the items

這是我的ViewModel類

namespace ERP_Lite_Trial.ViewModels
{
    public class GroupsViewModel : INotifyPropertyChanged
    {
        public GroupsViewModel()
        {
            using (DBEntities db = new DBEntities())
            {
                var groups = (from g in db.Groups
                              select g.Name).ToList();

                this.GroupName = groups;

                var correspondingEffects = (from g in db.Groups
                                            select g.Type_Effect.Name).ToList();

                this.EffectCorrespondingToGroup = correspondingEffects;
            }
    }

        private List<string> _groupName;
        public List<string> GroupName
        {
            get
            {
                return _groupName;
            }
            set
            {
                _groupName = value;
                OnPropertyChanged("GroupName");
            }
        }

        private List<string> _effectCorrespondingToGroup;
        public List<string> EffectCorrespondingToGroup
        {
            get
            {
                return _effectCorrespondingToGroup;
            }
            set
            {
                _effectCorrespondingToGroup = value;
                OnPropertyChanged("EffectCorrespondingToGroup");
            }
        }

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

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

現在,我將向您展示兩種情況:

情況1 :效果很好

<ComboBox x:Name="cbUnder" ItemsSource="{Binding Path=GroupName}" IsEditable="True"
            Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="3" />

在上述情況下,我從數據庫中獲取了所有組名,並正確顯示為comboBox的項目。 但這不是我想要的。 我想在此組合框中顯示兩列。

情況2 :未按預期工作(我可能犯了一些愚蠢的錯誤)

<ComboBox x:Name="cbUnder" IsEditable="True" Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="3">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=GroupName}" Width="100"/>
                <TextBlock Text="|" Width="10" />
                <TextBlock Text="{Binding Path=EffectCorrespondingToGroup}" Width="100"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

在這種情況下,我沒有收到任何錯誤,但組合框未顯示任何項目。

您的代碼需要創建兩個列表中當前擁有的信息,因為它們在一個列表中彼此相關。 作為兩個單獨的列表,無法將它們相互關聯。

首先更改您的數據庫查詢以將信息作為對象列表返回。

        using (DBEntities db = new DBEntities())
        {
            GroupsAndEffects = (from g in db.Groups
                          select new GroupAndEffect
                            {
                             GroupName = g.Name
                             EffectCorrespondingToGroup = g.Type_Effect.Name
                            }).ToList();
        }

var組必須是對象列表,而不是字符串列表:

private List<GroupAndEffect> _groupAndEffects;
    public List<GroupAndEffect> GroupsAndEffects
    {
        get
        {
            return _groupAndEffects;
        }
        set
        {
            _groupAndEffects = value;
            OnPropertyChanged("GroupsAndEffects");
        }
    }

需要一個GroupAndEffect類

public class GroupAndEffect
{
    public string GroupName;
    public string EffectCorrespondingToGroup;
}

更新案例2:

<ComboBox x:Name="cbUnder" ItemsSource="{Binding GroupsAndEffects}" IsEditable="True" Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="3">
<ComboBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding GroupName}"/>
            <TextBlock Text="|" Width="10" />
            <TextBlock Text="{Binding EffectCorrespondingToGroup}" Grid.Column="1" />
        </StackPanel>
    </DataTemplate>
</ComboBox.ItemTemplate></ComboBox>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM