we will run into wierd issue, if we try to use ComboBox in DataGrid (Silverlight RC0). you can find more details in this forum post
I think this will be addressed before Silverlight 2.0 is released. Here is a temporary workaround
I got all the code from the forum post, ( so I am not including the code)
Here is a workaround
Create a class, similar to this and set IsDropDownProperty to true in the loaded event
public class MyComboBox : ComboBox
{
public MyComboBox()
{
DefaultStyleKey = typeof(ComboBox);
this.Loaded += new RoutedEventHandler(MyComboBox_Loaded);
}
{
public MyComboBox()
{
DefaultStyleKey = typeof(ComboBox);
this.Loaded += new RoutedEventHandler(MyComboBox_Loaded);
}
void MyComboBox_Loaded(object sender, RoutedEventArgs e)
{
IsDropDownOpen = true;
}
}
{
IsDropDownOpen = true;
}
}
After declaring the Namespace
xmlns:src=”clr-namespace:xxx;assembly=xxx”
use the combobox like this
<data:DataGridTemplateColumn Header=”City”>
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text=”{Binding City}” />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
<data:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<src:MyComboBox SelectedItem=”{Binding City, Mode=TwoWay}”
ItemsSource=”{Binding CityList, Source={StaticResource cityProvider}}”
/>
</DataTemplate>
</data:DataGridTemplateColumn.CellEditingTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text=”{Binding City}” />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
<data:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<src:MyComboBox SelectedItem=”{Binding City, Mode=TwoWay}”
ItemsSource=”{Binding CityList, Source={StaticResource cityProvider}}”
/>
</DataTemplate>
</data:DataGridTemplateColumn.CellEditingTemplate>
</data:DataGridTemplateColumn>
You have done a greate job. But one thing you forgot to add DisplayMemberPath property , so the code will be :