gpt4 book ai didi

c# - 将 DatagridComboboxColumn 绑定(bind)到集合

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

我有这个 XAML

<DataGrid ItemsSource="{Binding Users}"
CanUserAddRows="False"
SelectedItem="{Binding SelectedUser}"
AutoGenerateColumns="False"
Grid.Column="0">
<DataGrid.Columns>
<DataGridTextColumn Header="First name" Binding ="{Binding FirstName}"/>
<DataGridTextColumn Header="Last name" Binding ="{Binding LastName}"/>
<DataGridComboBoxColumn Width="*" Header="Catégorie"
SelectedItemBinding="{Binding Role}">
<DataGridComboBoxColumn.ElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource"
Value="{Binding Path=DataContext.ComboBox_Roles,
RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type UserControl}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding
Path=DataContext.ComboBox_Roles, RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type UserControl}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>

我希望组合框绑定(bind)到我的角色集合
我的模型
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Role { get; set; }
}


在我的 View 模型中:
public ManageUsersViewModel()
{
_users = new ObservableCollection<User>
{
new User { FirstName = "First1", LastName = "Last1", Role = "Admin" },
new User { FirstName = "Last2", LastName = "Last2", Role ="User" },
};

ComboBox_Roles = new ObservableCollection<string> { "ABC", "BCD", "ASDSAD" };
}

public ObservableCollection<User> Users
{
get => _users;
set
{
_users = value;
}
}

public ObservableCollection<string> ComboBox_Roles { get; set; }

我怎样才能绑定(bind)这些元素?似乎问题是我不能以这种方式绑定(bind)到集合中的集合。我想要实现的是我有一个带有角色列表的组合框,我可以通过选择组合框的值之一来更改我的用户角色。 Upd:更新代码,因为我意识到我在做。

最佳答案

以下代码演示了一种绑定(bind) DataGridComboBoxColumn 的可能方法

<DataGrid ItemsSource="{Binding Users}" SelectedItem="{Binding SelectedUser}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="First name" Binding ="{Binding FirstName}"/>
<DataGridTextColumn Header="Last name" Binding ="{Binding LastName}"/>
<DataGridComboBoxColumn Width="*" Header="Catégorie" SelectedItemBinding="{Binding Role}">
<DataGridComboBoxColumn.ElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource"
Value="{Binding Path=DataContext.ComboBox_Roles,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource"
Value="{Binding Path=DataContext.ComboBox_Roles,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>

和 ViewModel
public class User {
public string FirstName { get; set; }
public string LastName { get; set; }
public string Role { get; set; }
}
public class ManageUsersViewModel {
public ManageUsersViewModel() {
Users = new ObservableCollection<User> {
new User { FirstName = "First1", LastName = "Last1", Role = "Admin" },
new User { FirstName = "Last2", LastName = "Last2", Role ="User" }};
ComboBox_Roles = new ObservableCollection<string> { "Admin", "User", "Other" };
}
public ObservableCollection<User> Users { get; set; }
public ObservableCollection<string> ComboBox_Roles { get; set; }
}

一个测试项目: https://github.com/Drreamer/GridComboBox

关于c# - 将 DatagridComboboxColumn 绑定(bind)到集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61518801/

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