gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:02:14 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com