如何从ListView绑定组合框的SelectedItem / Value / ValuePath

时间:2018-09-24 16:49:44

标签: c# wpf mvvm data-binding

我在ComboBox的{​​{1}}中有一个ListView。我无法正确获得GridView和SelectedItem / Value / ValuePath绑定。下面是展示我的问题的最小的完整示例:

Test_ComboBox_Binding.Views:

ItemsSource

Test_ComboBoxBinding.ViewModels:

<Window x:Class="Test_ComboBox_Binding.Views.UserView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        xmlns:m="clr-namespace:Test_ComboBox_Binding.Models"
        xmlns:vm="clr-namespace:Test_ComboBox_Binding.ViewModels"
        xmlns:s="clr-namespace:Test_ComboBox_Binding.Shared"
        Title="UserView" Height="300" Width="300">

    <Window.DataContext>
        <vm:UserViewModel/>
    </Window.DataContext>

    <Window.Resources>
        <DataTemplate x:Key="NameCellTemplate" DataType="m:cUser">
            <TextBlock Text="{Binding Name}"></TextBlock>
        </DataTemplate>       
        <DataTemplate x:Key="FavoriteColorCellTemplate" DataType="m:cUser">
            <ComboBox ItemsSource="{Binding Colors, RelativeSource={RelativeSource AncestorType=vm:UserViewModel}}"
                      SelectedValue="{Binding FavoriteColor}"
                      SelectedValuePath="{Binding FavoriteColor}"
                      MinWidth="60"/>
        </DataTemplate>        
    </Window.Resources>

    <Grid>
        <ListView ItemsSource="{Binding Users}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="100" CellTemplate="{StaticResource NameCellTemplate}"></GridViewColumn>
                    <GridViewColumn Header="Favorite Color" Width="100" CellTemplate="{StaticResource FavoriteColorCellTemplate}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>    
    </Grid>
</Window>

Test_ComboBox_Binding.Models:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using Test_ComboBox_Binding.Models;
using Test_ComboBox_Binding.Shared;

namespace Test_ComboBox_Binding.ViewModels
{
    class UserViewModel : cINotifyPropertyChangedBase
    {
        private ObservableCollection<ColorEnum> _colors;
        public ObservableCollection<ColorEnum> Colors
        {
            get { return _colors; }
            set { _colors = value; OnPropertyChanged("Colors"); }
        }

        private ObservableCollection<cUser> _users;
        public ObservableCollection<cUser> Users
        {
            get { return _users; }
            set { _users = value; OnPropertyChanged("Users");}
        }

        public UserViewModel()
        {
            Colors = new ObservableCollection<ColorEnum>(){ColorEnum.Red, ColorEnum.White, ColorEnum.Blue};

            List<cUser> userList = new List<cUser>();
            userList.Add(new cUser("Jack", Colors[0]));
            userList.Add(new cUser("Jill", Colors[1]));
            userList.Add(new cUser("James", Colors[2]));
            Users = new ObservableCollection<cUser>(userList);
        }
    }
}

Test_ComboBox_Binding.Shared:

using System.ComponentModel;
using Test_ComboBox_Binding.Shared;
using Test_ComboBox_Binding.ViewModels;

namespace Test_ComboBox_Binding.Models
{
    class cUser : cINotifyPropertyChangedBase
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged("Name"); }
        }

        private ColorEnum _favoriteColor;
        public ColorEnum FavoriteColor
        {
            get { return _favoriteColor; }
            set { _favoriteColor = value; OnPropertyChanged("FavoriteColor"); }
        }

        public cUser(string name, ColorEnum favoriteColor)
        {
            Name = name;
            FavoriteColor = favoriteColor;
        }
    }
}

运行上面的代码将产生下面的图像-一个namespace Test_ComboBox_Binding.Shared { public enum ColorEnum { Red, White, Blue } } 和一个ListView,其中正确包含枚举的ComboBox,但不会为每个{{ 1}}:

enter image description here

请教我如何正确设置ItemsSource和SelectedItem / Value / ValuePath。我已经在Internet上搜索了有关信息,无法解决此问题。我一定缺少一些关键的理解。谢谢!

2 个答案:

答案 0 :(得分:1)

我相信您的问题是您将FavoriteColor设置为与dataprovider绑定产生的对象不同的引用对象。尝试在UserViewModel中公开一个Enums集合,将组合框绑定到该集合(您可能需要使用FindAncestor来访问它),然后使用该集合中的引用来初始设置喜欢的颜色。

答案 1 :(得分:1)

gi的{​​{1}}是指可视树中元素的类型。您可以将其设置为AncestorType,然后绑定到RelativeSource的{​​{1}}的{​​{1}}属性,这是您的视图模型。

您还应该将ListBox属性绑定到Colors源属性。这应该起作用:

DataContext