Lionel Pire
Lionel Pire

Reputation: 368

Radiobutton does not generate object in Datagrid new Row

From my understanding, when using the "New row" in a Datagrid, it creates and object when one of its properties it set.

This works fine with DataGridTextColumn, the object is created when I write something in it. But I also have some DataGridTemplateColumn that contains each a radiobutton. When clicking them, it should generate the object since I'm setting a property of said object.

But it's not, as you can see on the next screenshot the first line is named, then I click one of the radio button, and everything is fine

Then on the second line, I'm only clicking the radio button, which should also create an object, but it's not, I can tell from being able to click multiples RadioButton of the same lines since the GroupName is initialized in the constructor

NOTE : The Xs are the radio button (X = checked, empty = unchecked)

enter image description here

As soon as I enter something in the text, the object is constructed, the GroupName is initialized and the radiobutton is set to its default value (first column)

enter image description here

I already checked if the TwoWay binding was working correctly by logging the "set" of the property linked to these radio buttons and it's working fine.

How can I force the object being constructed when I click the radio buttons?

EDIT : Things I tried :

So this has at least narrowed it to : How to trigger edit mode when clicking a DataGridTemplateColumn containing a RadioButton just like you'd to with a DataGridTextColumn by double clicking

EDIT 2 : As requested by comment, here is the XAML of my datagrid

<DataGrid ItemsSource="{Binding Theme.Competences}" AutoGenerateColumns="False" RowHeaderWidth="0"
  ColumnWidth="*" IsReadOnly="False" ColumnHeaderStyle="{StaticResource FullBorderDataGrid}" SelectionUnit="Cell"
  CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False">

    <DataGrid.Columns>
        <DataGridTextColumn Header="Compétence" Binding="{Binding Label}" />
        <DataGridTextColumn Header="Description" Binding="{Binding Description}" />
        <DataGridTemplateColumn Header="1" Width="10">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding GUID}" IsChecked="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                                                                Converter={l:EnumMatchToBooleanConverter},
                                                                ConverterParameter=One}" 
                                                                Style="{StaticResource XRadioButton}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="2" Width="10">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding GUID}" IsChecked="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                                                                Converter={l:EnumMatchToBooleanConverter},
                                                                ConverterParameter=Two}" 
                                                                Style="{StaticResource XRadioButton}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="3" Width="10">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding GUID}" IsChecked="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                                                                Converter={l:EnumMatchToBooleanConverter},
                                                                ConverterParameter=Three}" 
                                                                Style="{StaticResource XRadioButton}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="4" Width="10">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding GUID}" IsChecked="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                                                                Converter={l:EnumMatchToBooleanConverter},
                                                                ConverterParameter=Four}" 
                                                                Style="{StaticResource XRadioButton}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="NA" Width="10">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding GUID}" IsChecked="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                                                                Converter={l:EnumMatchToBooleanConverter},
                                                                ConverterParameter=NA}" 
                                                                Style="{StaticResource XRadioButton}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTextColumn Header="Commentaire" Binding="{Binding Comment}" />
    </DataGrid.Columns>
</DataGrid>

The model :

[ImplementPropertyChanged]
public class Competence : IEditableObject
{
    public int? Index { get; set; }
    public string Label { get; set; }
    public string Description { get; set; }
    public Value Value { get; set; } = Value.NA;

    public string GUID { get; set; }

    public Competence()
    {
        GUID = Guid.NewGuid().ToString();
    }

    void IEditableObject.BeginEdit()
    {
        Console.WriteLine("Begin edit caught");
    }

    void IEditableObject.CancelEdit()
    {

    }

    void IEditableObject.EndEdit()
    {

    }

    public string Comment { get; set; }
}

IEditableObject.BeginEdit is called when double clicking the DataGridTextColumn but never with DataGridTemplateColumn

Upvotes: 0

Views: 218

Answers (1)

NoName
NoName

Reputation: 8025

Add this to your datagrid:

<DataGrid AddingNewItem="dataGridView_AddingNewItem"....

And in the code:

void dataGridView_AddingNewItem(object sender, System.Windows.Controls.AddingNewItemEventArgs e)
{
    var item = new Competence();
    // Change item's values here.
    e.NewItem = item;
}

Upvotes: 1

Related Questions