This should be fairly simple thing to achieve yet
I am busting my head on this for way too long,
Why the ComboBox
control will display the list of categories while the DataGridComboBoxColumn
control refuse to display them when both use the same settings?
Do I have to specify cell templates for the DataGridComboBoxColumn in order to make it work or is there something I'm doing wrong here?
Steps to reproduce are simple as that:
1. create new wpf project and name it WpfApplication10
2. copy and paste the code into the MainWindow.xaml
and MainWindows.xaml.cs
.
3. run the project - you will notice the ComboBox
shows values while the DataGridComboBoxColumn
will not show values.
Example Code:
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfApplication10
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public Vm TheVm;
public MainWindow()
{
TheVm = new Vm
{
VmCategories = new ObservableCollection<VmCategory>
{
new VmCategory { CategoryName="Category1" },
new VmCategory { CategoryName="Category2" },
new VmCategory { CategoryName="Category3" },
new VmCategory { CategoryName="Category4" },
new VmCategory { CategoryName="Category5" },
new VmCategory { CategoryName="Category6" }
},
VmUsers = new ObservableCollection<VmUser>
{
new VmUser { Name = "Gil" },
new VmUser { Name = "Dan" },
new VmUser { Name = "John" },
}
};
InitializeComponent();
DataContext = TheVm;
}
}
public class Vm
{
public ObservableCollection<VmCategory> VmCategories { get; set; }
public ObservableCollection<VmUser> VmUsers { get; set; }
}
public class VmUser
{
public string Name { get; set; }
public VmCategory VmCategoryInfo { get; set; }
}
public class VmCategory
{
public string CategoryName { get; set; }
}
}
Example XAML:
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication10"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
d:DataContext="{d:DesignInstance local:Vm, IsDesignTimeCreatable=True}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ComboBox Width="200" Height="40"
ItemsSource="{Binding DataContext.VmCategories, RelativeSource={RelativeSource AncestorType=Window}}"
DisplayMemberPath="CategoryName" />
<DataGrid Grid.Row="1"
ItemsSource="{Binding VmUsers}" AutoGenerateColumns="False"
CanUserAddRows="True" CanUserDeleteRows="True" AreRowDetailsFrozen="False" SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridComboBoxColumn Header="Category" Width="120"
SelectedItemBinding="{Binding VmCategoryInfo}"
ItemsSource="{Binding DataContext.VmCategories, RelativeSource={RelativeSource AncestorType=Window}}"
DisplayMemberPath="CategoryName"
/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Important Update
To all others in the future who came to this page due to about same issue,
If you applied the solution and it still not solving your problem - it means you have another problem which causing the same issue - that is a devil spawn unless you figure it soon - I'll explain:
In my case, in the bigger program - not the example I provided,
The DataContext
I needed to address was in a TabItem
Control which itself is not part of the visual tree and so it didn't work, so actually two problems were there, one is the fact the DataGridColumns
are not in the visual tree and the other is that TabItem
is also not in the visual tree.. And so you apply a solution but problem remains and you think something is wrong with the solution while in fact unknowingly you are dealing with two problem which result in same bug.
I ended up solving it by creating a grid which served as proxy element to retain the DataContext
of the TabItem
.