我将类Devices with Platform作为其中一个属性:
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;
}
以及我的平台类:
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;
}
....
我有一个绑定到Devices类的DataGrid
,其中一列是我试图绑定到Platform类的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>
我看到了带有这些值的dropbox,但是在选择了任何值之后,当我尝试接收DataGrid.ItemsSource
列时,Platform是空的。我哪里做错了?我尝试将列更改为template with combobox inside -结果相同。我将感谢任何人的帮助,或者至少是深入研究的方向。
发布于 2015-11-30 05:01:34
如果您的平台对所有对象都是通用的,那么您可以将platforms属性设为静态的,并且您的xaml将使用它,如下所示:
<DataGridComboBoxColumn
Header="Role"
SelectedValueBinding="{Binding Role}"
ItemsSource="{Binding Source={x:Static local:ProjectsDataContext.Roles}}"
DisplayMemberPath="Name"/>
希望这能有所帮助。
https://stackoverflow.com/questions/16305612
复制相似问题