1

I have tried for days now to add a combo box in a data grid using MVVM and Caliburn.Micro.
I have tried almost every suggestion out I could find - no luck!

My Xaml is in a user control - Name and Test are showing fine!

XAML:

<DataGrid x:Name="Players" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="308">    
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Test" Binding="{Binding Position}"/>
        <DataGridComboBoxColumn x:Name="ComboBoxColumn" Header="Position"  SelectedItemBinding="{Binding Position}"/>
    </DataGrid.Columns>
</DataGrid>

ViewModel:

public class MissingCTypeViewModel : Screen, INotifyPropertyChanged
{
    public ObservableCollection<Player> Players { get; set; }
    public ObservableCollection<string> Positions { get; set; }

    public MissingCTypeViewModel()
    {

        Positions = new ObservableCollection<string>() { "Forward", "Defense", "Goalie" };
        Players = new ObservableCollection<Player>(){
                new Player() {Name = "Tom",Position= "Forward"},
                new Player() {Name = "Dick", Position= "Defense"},
                new Player() {Name = "Harry", Position= "Goalie"}
            };
        //ComboBoxColumn.ItemsSource = Positions;
        //dataGrid1.ItemsSource = Players;
    }
}

public class Player
{
    public string Name { set; get; }
    public string Position { set; get; }
}

Result:

https://i.stack.imgur.com/aGuUF.png

Hille
  • 2,123
  • 22
  • 39

1 Answers1

1

You're missing DataGridComboBoxColumn ItemsSource binding to Positions collection.

Note, that you need to have it as a static resource - see: Binding ItemsSource of a ComboBoxColumn in WPF DataGrid

XAML:

xmlns:vm="clr-namespace:TheNamespeceOfYourApp.ViewModels"

<DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
    <DataGridTextColumn Header="Test" Binding="{Binding Position}"/>
    <DataGridComboBoxColumn x:Name="ComboBoxColumn" Header="Position"  SelectedItemBinding="{Binding Position}" ItemsSource="{Binding Source={x:Static vm:MissingCTypeViewModel.Positions}}"/>
</DataGrid.Columns>

ViewModel:

public static List<string> Positions = new List<string>() { "Forward", "Defense", "Goalie" };`
Clemens
  • 123,504
  • 12
  • 155
  • 268
Miq
  • 3,931
  • 2
  • 18
  • 32
  • SelectedItem is not available in ? – Michael Guldbrand Apr 04 '18 at 10:28
  • Indeed, my bad, I mixed it with standard ComboBox. How did you bind ItemsSource? If you don't see any dropdown value, this is where I'd look for error. – Miq Apr 04 '18 at 10:36
  • I edited my post with working solution. please check. – Miq Apr 04 '18 at 11:13
  • Making Positions static is a workaround. However, it could still be a property: `public static List Positions { get; } = ...` – Clemens Apr 04 '18 at 11:28
  • That Worked :) `public static List Positions;` `Positions = new List();` `Positions.Add("Forward");` And so on... – Michael Guldbrand Apr 04 '18 at 12:00
  • remember that this is a static field, and if you will use Add method calls in non-static constructor, each time you will create view-model instance, you will add to the list another set of values. As @Clemens suggested using static field is a workaround and you either should have it initialized only one time (in static constructor), use enum or have it defined in static resource/separate static class. – Miq Apr 04 '18 at 12:05