In a WPF DataGrid right-clicking on a row selects the row. In this example of DataGrid with 2 Columns, when I right-click on a row of the DataGridTemplateColumn, nothing happens.
If I right-click on a row the DataGridTextColumn the row is selected and the Background Color is set to blue.
The strange thing is that if I right-click on the DataGridTemplateColumn once, then the selection row works correctly.
To reproduce it, first right-click on a row on the DataGridTemplateColumn, then right-click on another row and finally right-click again on the first clicked row on the DataGridTemplateColumn, and then the selection works.
How to get the row selection when right-clicking once on a row on the DataGridTemplateColumn ?
XAML
<DataGrid ItemsSource="{Binding SourceList}"
SelectedItem="{Binding SelectedSource}"
Height="auto"
IsReadOnly="False"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="NameText" Binding="{Binding NameText}" Width="*"/>
<DataGridTemplateColumn Header="Template" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ComboCheck}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="{Binding Program}" Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
THE VM
public class MainVM
{
private ObservableCollection<SelectableModelData> myNamesList = new ObservableCollection<SelectableModelData>()
{
new SelectableModelData(new ModelData<string>("name 1 obj", "name 1 text"), true),
new SelectableModelData(new ModelData<string>("name 2 obj", "name 2 text"), true),
new SelectableModelData(new ModelData<string>("name 3 obj", "name 3 text"), true)
};
private List<SourceTest> mySourceList;
public MainVM()
{
mySourceList = new List<SourceTest>();
SourceTest t = new SourceTest("Text 1", myNamesList[0], true, 0);
t.ComboCheck.Add(new Programs("yes", true));
t.ComboCheck.Add(new Programs("no", false));
mySourceList.Add(t);
t = new SourceTest("Text 2", myNamesList[1], true, 1);
t.ComboCheck.Add(new Programs("yes", true));
t.ComboCheck.Add(new Programs("no", false));
mySourceList.Add(t);
t = new SourceTest("Text 3", myNamesList[2], true, 2);
t.ComboCheck.Add(new Programs("yes", true));
t.ComboCheck.Add(new Programs("no", false));
mySourceList.Add(t);
}
public List<SourceTest> SourceList => mySourceList;
public SourceTest SelectedSource { get; set; }
}
ASSOCIATED CLASSES
public class Programs
{
public Programs(string prog, bool check)
{
Program = prog;
IsChecked = check;
}
public bool IsChecked { get; set; }
public string Program { get; set; }
}
public class SourceTest
{
public SourceTest(string text, SelectableModelData sourceSMD, bool male, int index)
{
SourceSMD = sourceSMD;
NameText = text;
Male = male;
Female = !male;
Index = index;
ComboCheck = new ObservableCollection<Programs>();
}
public SelectableModelData SourceSMD { get; set; }
public string NameText { get; set; }
public bool Male { get; set; }
public bool Female { get; set; }
public int Index { get; set; }
public bool IsAllowed { get; set; }
public ObservableCollection<Programs> ComboCheck { get; set; }
}
public class SelectableModelData : ObservableObject
{
private bool myIsSelectable;
public SelectableModelData(ModelData<string> name, bool isselectable)
{
SModelData = name;
IsSelectable = isselectable;
}
public bool IsSelectable
{
get { return myIsSelectable; }
set
{
if(myIsSelectable = value) return;
myIsSelectable = value;
OnPropertyChanged(nameof(IsSelectable));
}
}
public ModelData<string> SModelData { get; set; }
}
public class ModelData<T>
{
public ModelData() { }
public ModelData(T item, string name)
{
Item = item;
Name = name;
}
public T Item { get; private set; }
public string Name { get; set; }
}