У меня есть ComboBox
внутри ListView
х GridView
.Я не могу получить правильные привязки ItemsSource
и SelectedItem / Value / ValuePath.Ниже приведен самый маленький полный пример, демонстрирующий мою проблему:
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
}
}
Запуск приведенного выше кода приведет к изображению ниже - ListView
с ComboBox
, который правильно содержит перечислимый ItemsSource
, но не отображаетSelectedValue
для каждого cUser
:
![enter image description here](https://i.stack.imgur.com/xGfUk.png)
Пожалуйста, расскажите мне, как правильно установить ItemsSource и SelectedItem / Value / ValuePath.Я проверил Интернет для информации и не могу решить эту проблему.Я, должно быть, упускаю какое-то ключевое понимание.Спасибо!