wpf DataGridComboBoxColumn 绑定到 ObservableCollection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16305612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridComboBoxColumn binding to ObservableCollection
提问by Dmitry Z
I have the class Devices with Platform as one of the properties:
我将类设备与平台作为属性之一:
public partial class DevicesCollection : ObservableCollection<Device>
{
public DevicesCollection() : base()
{ }
}
public partial class Device : INotifyPropertyChanged
{
private string hostIP;
private string password;
private int platform;
private string status = "";
private int loop = 1;
public Device() { }
public Device(string ip, string pswd, int tp)
{
HostIP = ip;
Password = pswd;
Platform = tp;
Status = "Disconnected";
Loop = 1;
}
As well as I have Platform class:
以及我有平台类:
public partial class PlatformsCollection : ObservableCollection<Platform>
{
public PlatformsCollection()
: base()
{
Add(new Platform(1, "iOS"));
Add(new Platform(2, "Android"));
Add(new Platform(3, "Windows"));
Add(new Platform(4, "Blackberry"));
}
}
public partial class Platform : INotifyPropertyChanged
{
private string platformName;
private int platformId;
public Platform(int id, string name)
{
PlatformName = name;
PlatformId = id;
}
....
I have a DataGridwhich is bound to Devices class and one of the columns is a ComboBoxPlatform which I'm trying to bind to Platform class:
我有一个DataGrid绑定到设备类的列,其中一列是ComboBox我试图绑定到平台类的平台:
<DataGridComboBoxColumn x:Name="platform" Header="Platform" CanUserResize="False"
ItemsSource="{Binding Platform}"
SelectedValueBinding="{Binding Path=Platform.PlatformId}"
SelectedValuePath="PlatformId"
DisplayMemberPath="PlatformName" Width="100">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=Platform.PlatformName}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
I see the dropbox with the values, but after selecting any value when I trying to receive the DataGrid.ItemsSourcecolumn Platform is empty. What I'm doing wrong? I tried to change the column to template with combobox inside - same result. I'll appreciate any help or at least direction to dig in.
我看到带有值的保管箱,但在我尝试接收DataGrid.ItemsSource列平台时选择任何值后为空。我做错了什么?我试图将列更改为带有组合框的模板 - 结果相同。我将不胜感激任何帮助或至少是挖掘方向。
回答by Justin
Are you attempting to bind the DataGridComboBoxColumn's ItemsSourceproperty to an ObservableCollection(PlatformsCollection) or to a Property (Platform)?
您是否试图将DataGridComboBoxColumn的ItemsSource属性绑定到ObservableCollection(PlatformsCollection) 或属性 (Platform)?
You should be able to accomplish this with something like..
你应该能够用类似的东西来完成这个..
<DataGridComboBoxColumn Header="Platform" ItemsSource="{Binding PlatformsCollection}"
SelectedValue="{Binding SelectedPlatform}" DisplayMemberPath="PlatformName"/>
You'll need to add another member to your model called SelectedPlatform which will store/update each time the user changes the selected platform.
您需要将另一个名为 SelectedPlatform 的成员添加到您的模型中,该成员将在用户每次更改所选平台时存储/更新。
Additionally, you may want to look into using a CellTemplate/CellEditingTemplatecombination with a DataGridTemplateColumnfor this which looks a bit nicer. The user is only presented with a textbox unless they click into the cell at which point the combobox would appear.
此外,您可能需要考虑使用CellTemplate/CellEditingTemplate与 a 的组合,DataGridTemplateColumn这看起来更好一些。用户只会看到一个文本框,除非他们点击单元格,此时组合框会出现。
The markup for this would be something like
对此的标记将类似于
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding SelectedPlatform.PlatformName}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding PlatformsCollection}"
SelectedValue="{Binding SelectedPlatform}"
DisplayMemberPath="PlatformName"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn
Hopefully that helps. Let me know if you have any questions.
希望这有帮助。如果您有任何问题,请告诉我。
回答by George Lanetz
If your platforms are common for all objects, then you can make platforms property static and your xaml will use it as it's shown below:
如果您的平台对于所有对象都是通用的,那么您可以将平台属性设为静态,并且您的 xaml 将使用它,如下所示:
<DataGridComboBoxColumn
Header="Role"
SelectedValueBinding="{Binding Role}"
ItemsSource="{Binding Source={x:Static local:ProjectsDataContext.Roles}}"
DisplayMemberPath="Name"/>
Hopefully, it will help.
希望它会有所帮助。

