Reputation: 16959
How can I make DataGridComboBoxColumn display the user phone numbers (right now it displays a blank combobox)?
EDIT: Code below has been updated and now works.
<Window x:Class="DataGridTest.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:DataGridTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid
x:Name="myDataGrid"
Grid.Row="1"
AutoGenerateColumns="False"
CanUserAddRows="True"
ItemsSource="{Binding PersonList}"
>
<DataGrid.Columns>
<DataGridTextColumn
Header="LastName"
Binding="{Binding LastName}"/>
<DataGridTextColumn
Header="FirstName"
Binding="{Binding FirstName}" />
<DataGridComboBoxColumn
Header="Phone Number"
>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding PhoneNumbers}" />
<Setter Property="SelectedValue" Value="{Binding SelectedPhoneNumber}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding PhoneNumbers}" />
<Setter Property="SelectedValue" Value="{Binding SelectedPhoneNumber}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
// code behind
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DataGridTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<Person> PersonList { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
PersonList = new ObservableCollection<Person>();
Person jjim = new Person() { FirstName = "jimmy", LastName = "jim" };
jjim.PhoneNumbers.Add("123-4567");
jjim.PhoneNumbers.Add("234-5678");
Person mmark = new Person() { FirstName = "mike", LastName = "mark" };
mmark.PhoneNumbers.Add("345-6789");
mmark.PhoneNumbers.Add("456-7890");
PersonList.Add(jjim);
PersonList.Add(mmark);
}
}
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<string> PhoneNumbers { get; set; }
private string selectedPhoneNumber;
public string SelectedPhoneNumber
{
get
{
return this.selectedPhoneNumber;
}
set
{
this.selectedPhoneNumber = value;
RaisePropertyChanged("SelectedPhoneNumber");
}
}
public Person()
{
PhoneNumbers = new ObservableCollection<string>();
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string firstName;
private string lastName;
public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
RaisePropertyChanged("FirstName");
}
}
public string LastName
{
get
{
return this.lastName;
}
set
{
this.lastName = value;
RaisePropertyChanged("LastName");
}
}
}
}
Upvotes: 0
Views: 41
Reputation: 169200
A DataGridColumn
is not a visual element and doesn't have a DataContext
and therefore your binding won't work. You could easily fix this by specifying an ElementStyle
and and EditingElementStyle
for the DataGridComboBoxColumn
:
<DataGridComboBoxColumn Header="Phone Number">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding PhoneNumbers}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding PhoneNumbers}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
This works because the actual ComboBox
element that eventually gets added to the visual tree by the DataGridComboBoxColumn
and that the styles are getting applied to always has a DataContext
.
Please refer to the following link for more examples of how to data bind to a ComboBox
in WPF: https://code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82#content
Upvotes: 1
Reputation: 6155
Have a look at my anser to this question: DataGridComboBoxColumn is empty
Seems like the same problem. The DataGridComboBoxColumn
is a bit buggy.
Upvotes: 1