首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >到ObservableCollection的DataGridComboBoxColumn绑定

到ObservableCollection的DataGridComboBoxColumn绑定
EN

Stack Overflow用户
提问于 2013-05-01 01:24:26
回答 1查看 10.2K关注 0票数 2

我将类Devices with Platform作为其中一个属性:

代码语言:javascript
复制
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;
    }        

以及我的平台类:

代码语言:javascript
复制
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平台:

代码语言:javascript
复制
<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 -结果相同。我将感谢任何人的帮助,或者至少是深入研究的方向。

EN

回答 1

Stack Overflow用户

发布于 2015-11-30 05:01:34

如果您的平台对所有对象都是通用的,那么您可以将platforms属性设为静态的,并且您的xaml将使用它,如下所示:

代码语言:javascript
复制
<DataGridComboBoxColumn
    Header="Role"
    SelectedValueBinding="{Binding Role}"
    ItemsSource="{Binding Source={x:Static local:ProjectsDataContext.Roles}}"
    DisplayMemberPath="Name"/>

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16305612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档