开发者

How to bind a ListView to an ObservableCollection<KeyValuePair<int,object>> inside a ViewModel in WPF?

Something like thi开发者_如何学Cs:

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>.

<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.

Any ideas?


your actual binding will not work cause EffectViewModel has no key and Value Property. i really dont know what your listview should display. if you want a list of EffectViewModels then the Itemssource is right. if you want further for each EffectViewModel to display the settings. then you need somekind of itemsscontrol with Itemssource={Binding Settings}. this itemsscontrol of course will need a itemsstemplate with your Key and Value.

i have no VS here atm, but your GridViewColumn needs a kind of CellTemplate. and this template should consist of a itemscontrol. because you have 2collections! 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. one parent Combobox or listbox (x:Name=parent) with itemssource=EffectWindowViewModel.Effects and a second ListView like you have, with the itemssource binding:

ItemsSource="{Binding ElementName=parent, Path=SelectedItem.Settings}"


You could try:

ItemsSource="{Binding ElementName=Settings, Path=EffectWindowViewModel.Effects}"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜