c# - 如何从 ListView 中绑定(bind) Combobox 的 SelectedItem/Value/ValuePath

标签 c# wpf mvvm data-binding

我有一个 ComboBox ListView 的内部的GridView .我无法获得 ItemsSource并且 SelectedItem/Value/ValuePath 绑定(bind)正确。以下是展示我的问题的最小完整示例:

Test_ComboBox_Binding.Views:

<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_ComboBoxBinding.ViewModels:
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.Models:
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;
        }
    }
}

Test_ComboBox_Binding.Shared:
namespace Test_ComboBox_Binding.Shared
{
    public enum ColorEnum
    {
        Red,
        White,
        Blue
    }
}

运行上面的代码将产生下面的图像 - 一个 ListViewComboBox包含枚举的 ItemsSource正确,但未显示 SelectedValue对于每个 cUser :

enter image description here

请教我如何正确设置 ItemsSource 和 SelectedItem/Value/ValuePath。我已经在互联网上搜索了信息,但无法解决这个问题。我一定错过了一些关键的理解。谢谢!

最佳答案

AncestorTypeRelativeSource指视觉树中元素的类型。您可以将其设置为 ListBox然后绑定(bind)到Colors DataContext 的属性(property)的ListBox ,这是您的 View 模型。

您还应该绑定(bind) SelectedItem FavoriteColor 的属性(property)源属性。这应该有效:

<DataTemplate x:Key="FavoriteColorCellTemplate" DataType="m:cUser">
    <ComboBox ItemsSource="{Binding DataContext.Colors, RelativeSource={RelativeSource AncestorType=ListBox}}"
              SelectedItem="{Binding FavoriteColor}"
              MinWidth="60"/>
</DataTemplate>

关于c# - 如何从 ListView 中绑定(bind) Combobox 的 SelectedItem/Value/ValuePath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52483982/

相关文章:

C#:我可以没有项目入口点吗?

c# - 以样式定义 InputBindings

c# - 从 C# 调用 C DLL

c# - 使用c#表达式定义服务器标签属性值

c# - 在 WPF 中使用带有 MenuItem 的命令

wpf - VisualStateManager同时出现在WPF Toolkit和PresentationFramework中 - 如何解决

wpf - 验证命令仅允许在数据库中输入 1 行

Silverlight 如何将选定的数据网格行传递给 View 模型

c# - WPF MVVM 防止在某些情况下绑定(bind) SelectedValue 以更改值

c# - 试图打开文件,格式错误?