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. Last column of listview is a ComboBox that i need to bind Id of each instances of a groupmemory in it. 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. how can i write xaml code for combobox or its code behinde?
here is my xaml code for listview
<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 ItemsSource="{Binding Instances.Values}"
DisplayMemberPath="Id"
SelectedValuePath="Id"/>
Your Instances
is declared as a field and it's not a valid binding source . 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
<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.
Its relatively simple
Instances is a KeyValuePair, you will need to bind to Value.Id
However i also noticed that neither of your classes implement 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. http://blogs.microsoft.co.il/shimmy/2010/12/26/observabledictionarylttkey-tvaluegt-c/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.