简体   繁体   English

在WPF C#中填充ComboBox项

[英]Fill ComboBox Items in WPF C#

I Have two classes like these: 我有两个这样的课程:

public class Instance
{
    /// <summary>
    /// Identity of index
    /// </summary>
    public int Id { get; set; }

    /// <summary>
    /// Items within this group.
    /// </summary>
    public Dictionary<string, ItemRow> Items;

    public Instance()
    {
        //Items = new Dictionary<string, ItemRow>();
        Items = new Dictionary<string, ItemRow>();
    }
}

public class GroupMemory
{
    /// <summary>
    /// Name of the OPC group
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// Scan rate for this group.
    /// </summary>
    public string ScanRate { get; set; }
    /// <summary> 
    /// Number of instances for this group.
    /// </summary>
    public int CountOfInstances { get; set; }

    /// <summary>
    /// Instances within this group.
    /// </summary>
    public Dictionary<int, Instance> Instances;
    /// <summary>
    /// Items within this group.
    /// </summary>
    public Dictionary<string, ItemRow> Items;

    /// <summary>
    /// Constructor of GroupMemory class.
    /// </summary>
    public GroupMemory()
    {
        Items = new Dictionary<string, ItemRow>();
        Instances = new Dictionary<int, Instance>();
    }
}

I have a listview that i bind GroupMemory object in each item of listview. 我有一个列表视图,我在列表视图的每个项目中绑定了GroupMemory对象。 Last column of listview is a ComboBox that i need to bind Id of each instances of a groupmemory in it. listview的最后一列是ComboBox,我需要在其中组合groupmemory的每个实例的ID。 for example if each group memory contains two instances and instances have Id=1 and Id=2, i want to show 1 and 2 in combobox as its items. 例如,如果每个组内存包含两个实例,并且实例的Id = 1和Id = 2,我想在组合框中显示1和2作为其项。 how can i write xaml code for combobox or its code behinde? 我怎样才能为组合框或其背后的代码编写XAML代码?

here is my xaml code for listview 这是我的listview的xaml代码

<ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}, Converter={StaticResource starWidthConverter}}"  DisplayMemberBinding="{Binding Path=Name}" />
                <GridViewColumn Header="Scan Rate" Width="60" DisplayMemberBinding="{Binding Path=ScanRate}" />
                <GridViewColumn Header="Instances" Width="60" DisplayMemberBinding="{Binding Path=CountOfInstances}" />
                <GridViewColumn Header="Index" Width="100">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Width="50" ItemsSource="{Binding Instances}" SelectedValue="{Binding Id}" DisplayMemberPath="Id"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>

Assuming that DataContext of the ComboBox is an instance of GroupMemory class, you can try to bind ComboBox's ItemsSource to Instances.Values property and set DisplayMemberPath to display Id of each Instance : 假设ComboBox的DataContextGroupMemory类的实例,则可以尝试将ComboBox的ItemsSource绑定到Instances.Values属性,并将DisplayMemberPath设置为显示每个Instance Id

<ComboBox ItemsSource="{Binding Instances.Values}"
          DisplayMemberPath="Id"
          SelectedValuePath="Id"/>

Your Instances is declared as a field and it's not a valid binding source . 您的Instances被声明为字段,并且不是有效的绑定源 You should convert it to a property if you want to bind to it. 如果要绑定到属性,应将其转换为属性。 Change declaration to: 将声明更改为:

public Dictionary<int, Instance> Instances { get; set; }

and then, because it's a Dictionaty<K, V> , and each item is of a KeyValuePair<K, V> type where Key is your int and Value is your Instance try changing DisplayMemberPath to Value.Id 然后,因为它是Dictionaty<K, V> ,并且每个项目都是KeyValuePair<K, V>类型,其中Key是您的intValue是您的Instance尝试将DisplayMemberPath更改为Value.Id

<ComboBox ItemsSource="{Binding Instances}" DisplayMemberPath="Value.Id"/>

If you bind the itemsource to the Instances property, you could then bind the DisplayMember and/or valuemember to Key or Value. 如果将itemsource绑定到Instances属性,则可以将DisplayMember和/或valuemember绑定到Key或Value。

Its relatively simple 它比较简单

Instances is a KeyValuePair, you will need to bind to Value.Id 实例是一个KeyValuePair,您需要绑定到Value.Id

However i also noticed that neither of your classes implement INotifyPropertyChanged. 但是我也注意到您的两个类都不实现INotifyPropertyChanged。 You will need to implement this as this is how the Binding knows your values have been updated. 您将需要执行此操作,因为这是绑定如何知道您的值已更新的方式。

You may also need to check out the ObservableDictionary if you're stuck on using a dictionary. 如果您坚持使用字典,则可能还需要检出ObservableDictionary。 http://blogs.microsoft.co.il/shimmy/2010/12/26/observabledictionarylttkey-tvaluegt-c/ http://blogs.microsoft.co.il/shimmy/2010/12/26/observabledictionarylttkey-tvaluegt-c/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM