Data grid text column single-click edit mode
I solved it:
Following this tutorial: http://wpf.codeplex.com/wikipage?title=Single-Click%20Editing
I just changed my DataGridTemplateColumns to DataGridTextBoxColumns. In doing this, I had to get rid of the CellTemplate and CellEditingTemplate elements.
Now my column template looks like this:
<DataGridTextColumn
Width="60"
Binding="{Binding GelPakLocation}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock
Style="{StaticResource tbkStyleGridHeader}"
TextWrapping="Wrap"
Text="GelPak Location"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
Doing this also allowed me to delete this line in the PreviewMouseLeftButtonDown event handler:
cell.IsEditing = true;
It seems like this line didn't really do anything anyways.
WPF Datagrid single click row to open new Page
A better way to do it, is to define a collection of Person
that define the DataGrid
ItemSource
and a Property of type Person
that contains the selected Item in the DataGrid
:
<DataGrid x:Name="PersonDataGrid" AutoGenerateColumns="False"
SelectionMode="Single" SelectionUnit ="FullRow"
MouseRightButtonUp="PersonDataGrid_CellClicked"
ItemsSource="{Binding Persons}"
SelectedItem="{Binding SelectedPerson}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=PersonId}" Header="Id" />
<DataGridTextColumn Binding="{Binding Path=PersonName}" Header="Name" />
</DataGrid.Columns>
</DataGrid>
and the SelectedPerson
and Persons
are defined in the code behind of this page like so :
public partial class Page1 : Page,INotifyPropertyChanged
{
private ObservableCollection<Person> _persons ;
public ObservableCollection<Person> Persons
{
get
{
return _persons;
}
set
{
if (_persons == value)
{
return;
}
_persons = value;
OnPropertyChanged();
}
}
private Person _selectedPerson ;
public Person SelectedPerson
{
get
{
return _selectedPerson;
}
set
{
if (_selectedPerson == value)
{
return;
}
_selectedPerson = value;
OnPropertyChanged();
}
}
public Page1()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Person
{
public int PersonId { get; set; }
public string PersonName { get; set; }
}
the INotifyPropertyChanged
is used to notify the UI of any changes in the Properties.
You could use the SelectedPerson
Property or pass it to the new page, when you receive the MouseRightButtonUp
event for example in the DataGrid
:
private void PersonDataGrid_CellClicked(object sender, MouseButtonEventArgs e)
{
if (SelectedPerson == null)
return;
this.NavigationService.Navigate(new PersonProfile(SelectedPerson));
}
and you could change the PersonProfile
Page to receive a Person in its Constructor.
Single click edit on WPF datagrid , combobox template column
I Did this by setting the combobox Loaded Event
private void DataGridComboboxTemplate_Loaded(object sender, RoutedEventArgs e)
{
if (sender != null)
{
ComboBox cmb = sender as ComboBox;
cmb.IsDropDownOpen = true;
}
}
Related Topics
Winforms Radiobuttonlist Doesn't Exist
Unrolled Loop Works, for Loop Does Not Work
Advantages to Using Private Static Methods
Get a List of Distinct Values in List
Date Format Yyyy-Mm-Ddthh:Mm:Ssz
Why Can't I Have Protected Interface Members
@Html.Hiddenfor Does Not Work on Lists in ASP.NET MVC
Generating .Net Crash Dumps Automatically
Could We Save Delegates in a File (C#)
How to Display a File's Properties Dialog from C#
Does Entity Framework 4 Code First Have Support for Identity Generators Like Nhibernate
Persist Data by Programming Against Interface
Why Is Try {...} Finally {...} Good; Try {...} Catch{} Bad
Why Would I Bother to Use Task.Configureawait(Continueoncapturedcontext: False);
Are 2 Dimensional Lists Possible in C#
Extension Methods Syntax VS Query Syntax
How to List All Loaded Assemblies
How to Support Listbox Selecteditems Binding with Mvvm in a Navigable Application