[英]How to bind a ListView to an ObservableCollection<KeyValuePair<int,object>> inside a ViewModel in WPF?
Something like this: 像这样:
public class EffectViewModel
{
public string Name ...
ObservableCollection<KeyValuePair<int,object>> settings
public ObservableCollection<KeyValuePair<int,object>> Settings
{
get {return this.settings;}
set
{
this.settings = value;
this.RaisePropertyChanged ( "Settings" );
}
}
}
Right now I am trying to bind it like this: 现在,我试图像这样绑定它:
EffectWindowViewModel.Effects
is of type ObservableCollection<EffectViewModel>
. EffectWindowViewModel.Effects
的类型为ObservableCollection<EffectViewModel>
。
<ListView Width="1000"
Height="600"
ItemsSource="{Binding EffectWindowViewModel.Effects}">
<ListView.View>
<GridView>
<GridViewColumn Width="Auto"
DisplayMemberBinding="{Binding Key}"
Header="Name" />
<GridViewColumn Width="Auto"
DisplayMemberBinding="{Binding Value}"
Header="Value" />
</GridView>
</ListView.View>
</ListView>
But I don't know how to specify .Settings
property. 但是我不知道如何指定.Settings
属性。
Any ideas? 有任何想法吗?
your actual binding will not work cause EffectViewModel has no key and Value Property. 您实际的绑定将不起作用,因为EffectViewModel没有键和Value属性。 i really dont know what your listview should display. 我真的不知道您的listview应该显示什么。 if you want a list of EffectViewModels then the Itemssource is right. 如果您需要一个EffectViewModels列表,则Itemssource是正确的。 if you want further for each EffectViewModel to display the settings. 如果您想让每个EffectViewModel进一步显示设置。 then you need somekind of itemsscontrol with Itemssource={Binding Settings}. 那么您需要使用Itemssource = {Binding Settings}的某种itemsscontrol。 this itemsscontrol of course will need a itemsstemplate with your Key and Value. 当然,此itemscontrol需要一个包含您的键和值的itemstemplate。
i have no VS here atm, but your GridViewColumn needs a kind of CellTemplate. 我这里没有VS,但是您的GridViewColumn需要一种CellTemplate。 and this template should consist of a itemscontrol. 并且此模板应包含一个项控件。 because you have 2collections! 因为您有2个收藏! this code is probably not right but should take you in the right direction 该代码可能不正确,但应该将您带向正确的方向
<ListView Width="1000"
Height="600"
ItemsSource="{Binding EffectWindowViewModel.Effects}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Settings}">
<GridViewColumn.CellTemplate>
<DataTemplatex:Key="myCell4Settings">
<ListView ItemsSource="{Binding.}">
<ListView.View>
<GridView>
<GridViewColumn Width="Auto"
DisplayMemberBinding="{Binding Key}"
Header="Name" />
<GridViewColumn Width="Auto"
DisplayMemberBinding="{Binding Value}"
Header="Value" />
</GridView>
</ListView.View>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</ListView>
</DataTemplate>
</GridView>
</ListView.View>
btw you could also use 2 lists independent. 顺便说一句,您也可以使用2个独立的列表。 one parent Combobox or listbox (x:Name=parent) with itemssource=EffectWindowViewModel.Effects and a second ListView like you have, with the itemssource binding: 一个带itemssource = EffectWindowViewModel.Effects的父组合框或列表框(x:Name = parent)和另一个带itemssource绑定的ListView,如您所愿:
ItemsSource="{Binding ElementName=parent, Path=SelectedItem.Settings}"
您可以尝试:
ItemsSource="{Binding ElementName=Settings, Path=EffectWindowViewModel.Effects}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.