DataGridComboBoxColumn绑定到ObservableCollection [英] DataGridComboBoxColumn binding to ObservableCollection
问题描述
我将设备类与平台作为属性之一:
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;
}
以及我的Platform类:
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;
}
....
我有一个 DataGrid
绑定到Devices类,并且其中一列是我要绑定到Platform类的 ComboBox
Platform:
I have a DataGrid
which is bound to Devices class and one of the columns is a ComboBox
Platform which I'm trying to bind to Platform class:
<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>
我看到带有值的保管箱,但是当我尝试接收 DataGrid.ItemsSource
列时选择了任何值之后,平台为空.我做错了什么?我试图将列更改为内部带有组合框的模板-相同的结果.我将不胜感激,至少会有所帮助.
I see the dropbox with the values, but after selecting any value when I trying to receive the DataGrid.ItemsSource
column 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.
推荐答案
如果平台对所有对象都是通用的,则可以将platform属性设为静态,并且xaml将如下所示使用它:
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:
<DataGridComboBoxColumn
Header="Role"
SelectedValueBinding="{Binding Role}"
ItemsSource="{Binding Source={x:Static local:ProjectsDataContext.Roles}}"
DisplayMemberPath="Name"/>
希望会有所帮助.
这篇关于DataGridComboBoxColumn绑定到ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!