2

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 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>

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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Dmitry Z
  • 31
  • 1
  • 3
  • 6
  • I don't have much experience with the `DataGridComboBoxColumn`, but the `ItemsSource` should be bound to a collection of platforms instead of the device's single selected `Platform`. Also, see these questions for some known caveats: [Binding DataGridComboBoxColumn](http://stackoverflow.com/questions/16303114/binding-datagridcomboboxcolumn), and [How to bind collection to WPF:DataGridComboBoxColumn](http://stackoverflow.com/questions/2890156/how-to-bind-collection-to-wpfdatagridcomboboxcolumn) – Sphinxxx Apr 30 '13 at 22:14

2 Answers2

0

Are you attempting to bind the DataGridComboBoxColumn's ItemsSource property to an ObservableCollection (PlatformsCollection) or to a Property (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.

Additionally, you may want to look into using a CellTemplate/CellEditingTemplate combination with a DataGridTemplateColumn for 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.

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.

Theresa
  • 3,515
  • 10
  • 42
  • 47
Justin
  • 91
  • 1
  • 5
  • You are right - I tried to bind Property(Platfrom) - binding to PlatformsCollection is a recent change - when I figured out that binding to Platform doesn't work. SelectedPlatform has to be class like Platform? I'm appologise if the questions are stupid - I'm pretty new in WPF and in .NET development - all in all I'm a PHP developer who needs some help:) Regarding the CellTemplate - it looks like really elegant solution, but I have to use this columnt in adition to an exist combobox column or instead of? – Dmitry Z May 01 '13 at 04:28
  • Honestly speaking I don't need to use platform as a class - all I need it provide Platform to Device from the list of 4 item. Does exist any way to create it without all this tricks - some hardcoded list of items which generates dropdown combobox? – Dmitry Z May 01 '13 at 11:14
  • Oh, gotcha. Yeah you could simply add the complex type Platform to your device class and set the SelectedValue attribute on the combobox equal to Device.Platform. – Justin May 01 '13 at 13:48
0

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"/>

Hopefully, it will help.

George Lanetz
  • 300
  • 5
  • 18