Hi,@Jane Jie Chen. For the problem of clicking on a particular column to select a row and clicking on other columns to select cells, you could use the SelectionUnit property of the DataGrid.
Xaml:
<DataGrid x:Name="dg" ItemsSource="{Binding Items}" AutoGenerateColumns="False"
Height="300" PreviewMouseLeftButtonDown="DataGrid_PreviewMouseLeftButtonDown"
SelectionUnit="Cell">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Run" Width=" 80" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="FileName" Binding="{Binding FileName}"/>
<DataGridTemplateColumn Header="Temperatures">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}" />
<TextBox Text="{Binding Temperature}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Cycles" Binding="{Binding Cycles}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
Codebedhind:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public ObservableCollection<RowData> Items { get; } = new ObservableCollection<RowData>();
public MainWindow()
{
InitializeComponent();
DataContext= this;
// Sample data
Items.Add(new RowData { FileName="name1", Temperature = "20°C", Cycles = "5" });
Items.Add(new RowData { FileName = "name2", Temperature = "25°C", Cycles = "10" });
Items.Add(new RowData { FileName = "name3", Temperature = "30°C", Cycles = "15" });
}
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
this._isSelected = value;
OnPropertyChanged(nameof(IsSelected));
}
}
private void DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
DataGridCell cell = FindVisualParent<DataGridCell>(dep);
if (cell != null)
{
int columnIndex = cell.Column.DisplayIndex;
if (columnIndex == 0)
{
((DataGrid)sender).SelectionUnit = DataGridSelectionUnit.FullRow;
}
else
{
((DataGrid)sender).SelectionUnit = DataGridSelectionUnit.Cell;
}
}
}
private static T FindVisualParent<T>(DependencyObject obj) where T : DependencyObject
{
DependencyObject parent = VisualTreeHelper.GetParent(obj);
if (parent == null)
return null;
T parentT = parent as T;
return parentT ?? FindVisualParent<T>(parent);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class RowData : INotifyPropertyChanged
{
private bool _isChecked;
private string _temperature;
private string _fileName;
private string _cycles;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
OnPropertyChanged(nameof(IsChecked));
}
}
public string Temperature
{
get { return _temperature; }
set
{
_temperature = value;
OnPropertyChanged(nameof(Temperature));
}
}
public string FileName
{
get { return _fileName; }
set
{
_fileName = value;
OnPropertyChanged(nameof(FileName));
}
}
public string Cycles
{
get { return _cycles; }
set
{
_cycles = value;
OnPropertyChanged(nameof(Cycles));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.