gpt4 book ai didi

c# - 绑定(bind) DataGridComboBoxColumn

转载 作者:行者123 更新时间:2023-11-30 20:02:48 27 4
gpt4 key购买 nike

我正在尝试将 T 的 ObservableCollection 绑定(bind)到 DataGrid 的 DataGridComboBoxColumn。
DataGrid 定义是:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Model, IsAsync=True}">

<DataGrid.Columns>
<DataGridTextColumn Header="Column Entry" IsReadOnly="True" Binding="{Binding ColumnName}"/>
<DataGridComboBoxColumn Header="Road Type" ItemsSource="{Binding RoadTypes}"/>
</DataGrid.Columns>

</DataGrid>

这是 ViewModel 和模型

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var viewModel = new ViewModel();
DataContext = viewModel;
}
}

public class ViewModel : ViewModelBase
{
private ObservableCollection<Model> _model;

public ViewModel()
{
var list = new List<Model>();
var roadTypes = new ObservableCollection<RoadType>
{
new RoadType
{
Code = 1,
Id = 1,
Name = "Name1"
},
new RoadType
{
Code = 1,
Id = 1,
Name = "Name1"
}
};

Model = new ObservableCollection<Model>
{
new Model
{
ColumnName = "Col1",
RoadTypes = roadTypes
},
new Model
{
ColumnName = "Col1",
RoadTypes = roadTypes
}
};
}

public ObservableCollection<Model> Model
{
get { return _model; }
set
{
_model = value;
RaisePropertyChanged(() => Model);
}
}
}

public class RoadType
{
public int Id { get; set; }
public int Code { get; set; }

public string Name { get; set; }
}

public class Model : ObservableObject
{
private ObservableCollection<RoadType> _roadTypes;
public string ColumnName { get; set; }

public ObservableCollection<RoadType> RoadTypes
{
get { return _roadTypes; }
set
{
_roadTypes = value;
RaisePropertyChanged(() => RoadTypes);
}
}
}

DataGrid 也显示文本列,但不显示 ComboBox 值。
怎么了?

最佳答案

由于 RoadTypes 不是一个简单的字符串列表,您需要告诉您的组合框它需要在组合框中显示什么属性。尝试添加

DisplayMemberPath="Name" 

到你的组合框声明

--

更新:

好的,这是 WPF 数据网格的一个已知“功能”。问题是 DataGridComboBox 没有 DataGrid 的 DataContext。我将 ComboBox 的绑定(bind)修改为如下所示:

<DataGridComboBoxColumn DisplayMemberPath="Name">
<DataGridComboBoxColumn.ElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding Path=RoadTypes}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding Path=RoadTypes}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

我修改了您在下载链接中提供的代码,当我打开组合框下拉菜单时显示了组合框项目。

查看其中一些链接以进一步说明:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b4b13a72-47f9-452f-85c6-6c4b5b606df5/

How to bind collection to WPF:DataGridComboBoxColumn

Excedrin headache #3.5.40128.1: Using combo boxes with the WPF DataGrid

促使我查看所有这些站点的原因是查看输出窗口并注意到错误消息 System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element.留言

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

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