0

I want to show a cancel button, when row is in edit mode.

Editing DataGrid row containing a cancel changes button source code

I can't find what I should bind to.

<DataGridTemplateColumn.CellTemplate>
...
    <Button Content="✖" Visibility="{Binding ????}" />

I find that we can implement additional markup extension, SO: "Code to check if a cell of a DataGrid is currently edited", but is there, a simplest solution?

marbel82
  • 925
  • 1
  • 18
  • 39
  • 1
    Also you can set visibility of something based on a trigger. If the `textBox` has focus, do X. – David Bentley Jul 25 '19 at 14:58
  • Please mark a given answer as accepted if it helped solving your problem. If none of the given answers helped, please further clarrify your problem. – Rand Random Jul 26 '19 at 09:33

3 Answers3

0

Instead of using the CellTemplate use the CellEditingTemplate

See the following description for the CellEditingTemplate:

The template that is used to display the contents of a cell that is in editing mode. The registered default is null. For information about what can influence the value, see

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • Thanks, CellEditingTemplate would be ok, if I wanted to do cell cancellation. I would prefer RowEditingTemplate if it existed. – marbel82 Jul 26 '19 at 09:43
0

You can use IsEditing property of DataGridCell to know if the cell in is edit mode. Bind this property to the Visibility of the button.

Aakanksha
  • 329
  • 2
  • 7
0

The solution is:

<Button Content="✖" Visibility="{Binding IsEditing, 
    RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, 
    Converter={StaticResource BoolToVisibilityConverter}}"

Just look at Live Visual Tree. By RelativeSource Mode=FindAncestor you can move up to find DataGridRow, and this one contains IsEditing property.

enter image description here

marbel82
  • 925
  • 1
  • 18
  • 39
  • I had answered the same thing i guess. What is the change here? – Aakanksha Aug 08 '19 at 10:38
  • Which cell should I bind to if I have 3 in a row. I have to bind to DataGridRow not DataGridCell. My question is "...show button when row is editing" – marbel82 Aug 08 '19 at 14:26