Single Click Edit in Wpf Datagrid

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



Leave a reply



Submit