i'm new to WPF and i just learned today data binding, so i may have some basics errors.
Goal : I want a column in the data grid that will function like a combo box column that shows all the items from a string list.
XAML :
<DataGrid x:Name="dataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="CitiesColumn" ItemsSource="{Binding Cities}/">
</DataGrid.Columns>
</DataGrid>
Code-Behind :
ObjectModel type :
class CitiesModel
{
private List<string> _Cities;
public List<string> Cities { get { return _Cities; } set { _Cities = value ; } }
public CitiesModel()
{
Cities = new List<string>
{
"Berlin",
"Rome",
"Paris",
"Barcelona"
};
}
}
window.cs file :
public partial class MainWindow : Window
{
CitiesModel Cm;
public MainWindow()
{
Cm = new CitiesModel();
DataContext = Cm;
InitializeComponent();
}
}
It shows no data at all not even a cell in the grid.. and when i tried the same code on a regular combo box it showed all of the data
XAML :
<ComboBox ItemsSource="{Binding Cities}"/>
I searched online and saw a way to do it with a DataGridTemplateColumn
instead of a ComboBox Column, and in this way i cant even see the column header.
XAML:
<DataGrid Margin="44,65,52,-24" AutoGenerateColumns="False">
<DataGridTemplateColumn Header="Street Address">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Cities}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
thanks.
答案 0 :(得分:0)
ItemsSource
of the DataGrid
, otherwise there won't even be any items.DataContext
than the grid itself, namely the context is the current row. So if you want to bind to another context you have to explicitly change the binding source.