Reputation: 3211
I have a DataGrid named data_grid_1, which has two columns header1 and header2 and is filled with SomeClass-object's inside the data_grid_1 Loaded-event-handler.
XAML-Code:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid x:Name="data_grid_1"
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Height="303" Width="497"
Loaded="data_grid_1_Loaded">
<DataGrid.Columns>
<DataGridTextColumn Header="header1"
Binding="{Binding some_field_variable_2, Mode=OneWay}" />
<DataGridTextColumn Header="header2"
Binding="{Binding some_field_variable_3, Mode=OneWay}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
XAML.CS-Code:
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void data_grid_1_Loaded(object sender, RoutedEventArgs e)
{
data_grid_1.Items.Add(new SomeClass(1, "data_grid_1-String1", "data_grid_1-Text1"));
data_grid_1.Items.Add(new SomeClass(2, "data_grid_1-String2", "data_grid_1-Text2"));
data_grid_1.Items.Add(new SomeClass(3, "data_grid_1-String3", "data_grid_1-Text3"));
data_grid_1.Items.Add(new SomeClass(4, "data_grid_1-String4", "data_grid_1-Text4"));
}
}
}
SomeClass.cs
namespace WpfApplication1
{
class SomeClass
{
public int some_field_variable_1 { get; internal set; }
public string some_field_variable_2 { get; internal set; }
public string some_field_variable_3 { get; internal set; }
public SomeClass(int some_field_variable_1,
string some_field_variable_2,
string some_field_variable_3)
{
this.some_field_variable_1 = some_field_variable_1;
this.some_field_variable_2 = some_field_variable_2;
this.some_field_variable_3 = some_field_variable_3;
}
}
}
I want to make the header2-column editable, so if the user clicks and changes the value of a row in header2-column the changes are written into the SomeClass-object, which were previously added inside data_grid_1_Loaded.
How can i make the second column editable?
Upvotes: 1
Views: 5184
Reputation: 73
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static ObservableCollection<SomeClass> GetItems()
{
ObservableCollection<SomeClass> some_inner_object_list = new ObservableCollection<SomeClass>();
some_inner_object_list.Add(new SomeClass(1, "data_grid_1-String1", "data_grid_1-Text1"));
some_inner_object_list.Add(new SomeClass(2, "data_grid_1-String2", "data_grid_1-Text2"));
some_inner_object_list.Add(new SomeClass(3, "data_grid_1-String3", "data_grid_1-Text3"));
some_inner_object_list.Add(new SomeClass(4, "data_grid_1-String4", "data_grid_1-Text4"));
return some_inner_object_list;
}
private void data_grid_1_Loaded(object sender, RoutedEventArgs e)
{
data_grid_1.ItemsSource = GetItems();
}
private void data_grid_1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (e.AddedCells.Count == 0) return;
var currentCell = e.AddedCells[0];
if (currentCell.Column ==
data_grid_1.Columns[1])
{
data_grid_1.BeginEdit();
}
}
}
<DataGrid x:Name="data_grid_1"
AutoGenerateColumns="False"
CanUserAddRows="False"
SelectionMode="Extended"
SelectionUnit="Cell"
Loaded="data_grid_1_Loaded"
SelectedCellsChanged="data_grid_1_SelectedCellsChanged" Margin="0,12,12,77">
<DataGrid.Columns>
<DataGridTextColumn Header="header1" Binding="{Binding some_field_variable_2, Mode=OneWay}" Width="1*" />
<DataGridTemplateColumn Header="header2" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding some_field_variable_3}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBox Text="{Binding some_field_variable_3}" />
<TextBlock Grid.Column="1" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Upvotes: 3
Reputation: 45
just using DataGridTextColumn.CellStyle
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<TextBox>//Binding your logic here
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
hope it helps..:)
Upvotes: 0