Hi, if you show Combobox in CellTemplate each select (in edit mode) take effect in every row. You must use CellTemplate with TextBlock and CellEditingTemplate with Combobox. If you display Description and use ID for reference beetween Master and Child you must convert in TextBlock binded value.
Try following demo:
<Window x:Class="WpfApp1.Window43"
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:WpfApp43"
mc:Ignorable="d"
Title="Window43" Height="450" Width="800" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="StatusItems"/>
<local:Converter x:Key="conv"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="dglist" PreviewMouseLeftButtonDown="dglist_PreviewMouseLeftButtonDown">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Combo">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Codigo, Converter={StaticResource conv}, ConverterParameter={StaticResource StatusItems}}" />
<Polyline Points="5 5 8 8 11 5" Stroke="Black" HorizontalAlignment="Right" Margin="0 0 5 0"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox BorderThickness="0"
IsDropDownOpen="True"
ItemsSource="{Binding Source={StaticResource StatusItems}}"
DisplayMemberPath="Descripcion"
SelectedValuePath="Codigo"
SelectedValue="{Binding Codigo, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Code:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using WpfApp43;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for Window43.xaml
/// </summary>
public partial class Window43 : Window
{
public Window43()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadEmployee();
}
public List<GenderList> GenderList { get; set; }
public List<Employee> Employees { get; set; }
void LoadEmployee()
{
GenderList = new List<GenderList>() {
new GenderList() { Codigo = 1, Descripcion = "MALE" },
new GenderList() { Codigo = 2, Descripcion = "FEMALE" } };
Employees = new List<Employee>() {
new Employee() { Codigo = 1, Name = "CARLOS", Gender = "MALE", Birthday = new DateTime(1971, 7, 23)},
new Employee() { Codigo = 2, Name = "ANDREA", Gender = "FEMALE", Birthday = new DateTime(1971, 7, 23)},
new Employee() { Codigo = 2, Name = "JOSE", Gender = "FEMALE", Birthday = new DateTime(1971, 7, 23)},
new Employee() { Codigo = 2, Name = "CHICHI", Gender = "FEMALE", Birthday = new DateTime(1971, 7, 23)},
new Employee() { Codigo = 2, Name = "MILU", Gender = "FEMALE", Birthday = new DateTime(1971, 7, 23)},
new Employee() { Codigo = 2, Name = "MIA", Gender = "FEMALE", Birthday = new DateTime(1971, 7, 23)}};
dglist.ItemsSource = Employees;
var cvs = (FindResource("StatusItems") as CollectionViewSource);
cvs.Source = GenderList;
}
public class Employee
{
public int Codigo { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public DateTime Birthday { get; set; }
}
private void dglist_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var mbe = e as MouseButtonEventArgs;
DependencyObject obj = null;
obj = mbe?.OriginalSource as DependencyObject;
while (obj != null && !(obj is DataGridCell)) obj = VisualTreeHelper.GetParent(obj);
DataGridCell cell = null;
DataGrid dataGrid = null;
if (obj != null) cell = obj as DataGridCell;
if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
{
if (!cell.IsFocused) cell.Focus();
dataGrid = FindVisualParent<DataGrid>(cell);
if (dataGrid != null)
{
if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
{
if (!cell.IsSelected) cell.IsSelected = true;
}
else
{
var row = FindVisualParent<DataGridRow>(cell);
if (row != null && !row.IsSelected) row.IsSelected = true;
}
}
}
}
private T FindVisualParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element;
while (parent != null)
{
T correctlyTyped = parent as T;
if (correctlyTyped != null) return correctlyTyped;
parent = VisualTreeHelper.GetParent(parent) as UIElement;
}
return null;
}
}
}
namespace WpfApp43
{
public class GenderList
{
public int Codigo { get; set; }
public string Descripcion { get; set; }
}
public class Converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
CollectionViewSource cvs = parameter as CollectionViewSource;
var col = cvs.Source as List<GenderList>;
return col.Where((item) => item.Codigo == (int)value).FirstOrDefault().Descripcion;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}