1

Problem Description

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;
    }
}
}

Question

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?

kiltek
  • 3,183
  • 6
  • 47
  • 70
  • 2
    Try removing the OneWay binding mode in the ``, and make sure your properties in `SomeClass` have a public set method. Right now they are set to `internal` – Rachel Jun 02 '15 at 21:14
  • This post might help: http://stackoverflow.com/questions/4471934/can-i-specify-which-columns-are-editable-in-a-wpf-datagrid – Kala J Jun 02 '15 at 21:19
  • 1
    @Rachel By removing `Mode=OneWay` and making setters public, double-clicking inside a grid results in a `InvalidOperationException: 'EditItem' is not allowed for this view`. @Kala J: I have seen that post, but i was not able to adapt it into my code. Can you help? – kiltek Jun 03 '15 at 07:09
  • @kiltek According to [this post](http://stackoverflow.com/q/6949022/302677), you'll need to create a `List` of `SomeClass` and set it as the `DataGrid.ItemSource` to make it properly support editing. `List` supports .Add and .Remove, which is needed. – Rachel Jun 03 '15 at 12:35

2 Answers2

3
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();
        }
    }
}

XAML-Code:

<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>
0

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..:)

Yang Kul
  • 45
  • 1
  • 10