Home Forums WPF controls Xceed DataGrid for WPF Enter editing mode

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #22368 |

    Hello,
    I create the columns in my grid dynamically in C#.

    The code looks like this:

    ObservableCollection<MyCell[]> data;
    … // fill the data
    myGrid.ItemsSource = data;
    foreach (MyColumn mc in MyColumnList) {
    Binding b = new Binding();
    b.Path = new PropertyPath(“[” + MyColumnList.IndexOf(fc) + “].Value”);
    Column c = new Column(mc.Name, mc.Title, b);
    <b>c.CellEditor = CellEditor.TextBoxEditor;</b>
    myGrid.Columns.Add(c);
    }

    <xcdg:DataGridControl x:Name=”myGrid”
    <b>AutoCreateColumns=”False”
    EditTriggers=”None”
    CellEditorDisplayConditions=”CellIsCurrent” </b> />
    The data is displayed OK, but when I enter the cell, I still can’t start editing.
    What’s wrong with this? Should I programtically flag my cells as editable? How?

    Thanks in advance.

    Imported from legacy forums. Posted by Grisha (had 5154 views)

    Xceed Support
    Member
    Post count: 5658

    Your DisplayMemberBinding need to be a TwoWay binding for In-Place edition to work.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 433 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank you, but this is probably not enough.
    I’ve added this line to the code above:
    <b>b.Mode = BindingMode.TwoWay;</b>

    Now when I try to edit the cell, I got the following:
    <i>
    Unhandled Exception: System.ArgumentNullException: Value cannot be null.
    Parameter name: key
    at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
    at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
    at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
    at Xceed.Wpf.DataGrid.Cell.GetCurrentCellState()
    at Xceed.Wpf.DataGrid.Cell.SetIsBeingEdited(Boolean value)
    at Xceed.Wpf.DataGrid.Cell.BeginEdit()
    </i>
    ….

    What am I missing now?
    Thanks.

    Imported from legacy forums. Posted by Grisha (had 646 views)

    Xceed Support
    Member
    Post count: 5658

    Is it possible that the “mc.Name” for any of the MyColumn object is null?

    Imported from legacy forums. Posted by Marcus [Xceed] (had 406 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank you very much.
    This was indeed my fault (though more meaningful error message would help me a lot in this case!).

    Now I see that I have another problem:
    1. I’m using DataTemplate to define how the cell will look like. I want to use two different templates – one for editing and one for displaying.
    Some attributes (for example, cell’s background) are set according to the application logic and connected to object’s properties.
    The content of the cell is also a property (named Value).
    The problem is that I can’t determine what kind of binding I need.
    I tried two different approaches – first is to bind to the “Value” property:
    <b>Binding b = new Binding();
    b.Path = new PropertyPath(“[” + MyColumnList.IndexOf(mc) + “].Value”);
    Column c = new Column(mc.Name, mc.Title, b);
    c.CellContentTemplate = Resources[“display”] as DataTemplate;
    CellEditor e = Resources[“editor”] as CellEditor;</b>
    ——
    <b><DataTemplate x:Key=”display”>
    <TextBlock Background=”{Binding Path=BgColor}” Text=”{Binding}”/>
    </DataTemplate>
    <xcdg:CellEditor x:Key=”editor”>
    <xcdg:CellEditor.EditTemplate>
    <DataTemplate>
    <TextBox Background=”Yellow” Text=”{xcdg:CellEditorBinding}”/>
    </DataTemplate>
    </xcdg:CellEditor.EditTemplate>
    </xcdg:CellEditor></b>

    In this case the text is displayed and can be edited but I got an error that “BgColor property not found on object ‘String’ “.

    Second approach is to bind to the whole data object, which is actually much more suitable for me:
    <b>
    b.Path = new PropertyPath(“[” + MyColumnList.IndexOf(mc) + “]”); // without “.Value”
    —-
    <b><DataTemplate x:Key=”display”>
    <!—Now text is bound to Value property here and not in code—>
    <TextBlock Background=”{Binding Path=BgColor}” Text=”{Binding Path=Value, Mode=TwoWay}”/>
    </DataTemplate>
    <xcdg:CellEditor x:Key=”editor”>
    <xcdg:CellEditor.EditTemplate>
    <DataTemplate>
    <TextBox Background=”Yellow” Text=”{xcdg:CellEditorBinding}”/>
    </DataTemplate>
    </xcdg:CellEditor.EditTemplate>
    </xcdg:CellEditor></b>

    Now cells are displayed OK with correct background, but when I try to edit it I get toString() of my class instead of the text.
    What I need is probably something like
    Text = “{xcdg:CellEditorBinding <i>Path=Value</i>}”, but CellEditorBinding doesn’t have Path…
    I guess that I can set a path to CellEditorBinding in code, but I just don’t know how.

    And one more problem:
    I want to start editing immediately when the cell becomes current. So I put EditTriggers property of the grid to “None”. I see indeed that when I enter the cell, the template changes to that of the editor. But I still can’t start editing until I click in the cell one more time – the cursor isn’t seen and the cell doesn’t seem to accept input. Why?

    Sorry for the long message. I hope the answer is shorter… 🙂

    Imported from legacy forums. Posted by Grisha (had 657 views)

    Xceed Support
    Member
    Post count: 5658

    For your specific case, since you want to display a complex object but edit a sub-part of the object, you can replace the “{xcdgCellEditorBinding}” statement by the following binding:

    “{Binding RelativeSource={RelativeSource AncestorType={x:Type xcdg:Cell}}, Path=ActualContent.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}”

    There is 2 possible reasons for your other problem: First, to start editing as soon as the cell becomes current, it is best to use the “CellIsCurrent” flag for the EditTriggers property.

    Also, if you designed your CellEditor yourself, you need to specify which control need to receive focus when the cell begins edition. You can do so by setting the “DataCell.IsCellFocusScope” attached property to true on the TextBox:

    <DataTemplate>
    <TextBox Background=”Yellow” Text=”{xcdg:CellEditorBinding}” xcdg:DataCell.IsCellFocusScope=”True”/>
    </DataTemplate>

    Imported from legacy forums. Posted by Marcus [Xceed] (had 560 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank you very much. It works just fine!

    Imported from legacy forums. Posted by Grisha (had 6257 views)

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.