简体   繁体   English

如何将ListView绑定到ObservableCollection <KeyValuePair<int,object> &gt;在WPF中的ViewModel中?

[英]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.

相关问题 我如何绑定列表<keyvaluepair<myclass2,int> > 内部 ObservableCollection<myclass> ? </myclass></keyvaluepair<myclass2,int> - How I can bind a List<KeyValuePair<MyClass2,int>> inside ObservableCollection<MyClass>? 绑定到ObservableCollection的“值” <KeyValuePair<object, string> &gt; - Bind to the “value” of ObservableCollection<KeyValuePair<object, string>> 如何重新编号ObservableCollection <KeyValuePair<int, string> - How to renumber an ObservableCollection<KeyValuePair<int, string> 如何对ObservableCollection进行排序<KeyValuePair<int, string> - How to sort an ObservableCollection<KeyValuePair<int, string> 如何将带有contextMenu的WPF ListView绑定到viewModel - How to bind a WPF ListView with contextMenu to viewModel 如何将ObservableCollection绑定到ListView? - How to bind ObservableCollection to ListView? 无法将ViewModel ObservableCollection绑定到ListView - Can't bind a ViewModel ObservableCollection to listview 无法在MVF中将ObservableCollection从MVVM绑定到ListView - Unable to Bind ObservableCollection from MVVM to ListView in WPF 如何将ObservableCollection绑定到UserControl中的ListView? - How to bind ObservableCollection to the ListView in a UserControl? 如何在WPF中使用ObservableCollection绑定ItemsSource - How to Bind ItemsSource with ObservableCollection in WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM