开发者

DataGrid generating newrow when checking the radiobutton

I'm trying to use a datagrid binded to a collection of objects with two parameters. The datagrid has to allow me to insert new rows, thus to create new objects.

The first column, binded to the first parameter, is a text column. The second one, should be a template column, containing a radiobutton which uses a BoolToInt converter (not relevant) for setting the value to the second property.

When displaying the already existing rows in the grid, it also displays an empty row used for inserting a new object. Thus, we have two posibilities of inserting a new object in the list/grid: 1. We first type something in the text column and then we check the radiobutton 2. First we check the radiobutton and then we type something in the text column

The first option, works just fine. The second one doesn't: When i check the radiobutton and then go and type something in the text column, the radiobutton just gets unchecked. Actually it disappears because when I start typing text, a new row is created in the grid, with a new radiobutton. The one I first checked is just the one on the empty row allowing me to add new rows and it keeps going to the bottom of the list whenever i insert a new row.

I think this should be solved by forcing the grid to create a new row when checking the radiobutton, just as it does when typing in a text column.

This is the datagrid:

<DataGrid ItemsSource="{Binding Path=Rows}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=param1, TargetNullValue=''}">
            </DataGridTextColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <RadioButton VerticalAlignment="Center" IsChecked="{Binding Path=param2, Converter={StaticResource RPLBoolToIntConverter}, ConverterParameter=1}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>            
    </DataGrid>

And this is the code behind, creating some objects to display:

CPL cpl = new CPL();            
        cpl.Rows = new List<CPLRow>()
        {
            new CPLRow(){ param1=80001, param2=20304},
            new CPLRow(){ param1=80002, param2=20304},
            new CPLRow(){ param1=80003, param2=20304}
        开发者_StackOverflow社区};
        DataContext = cpl;

Is there any way to make it react to checking the radiobutton. I'm thinking of an example where I would like to just use radiobuttons and whenever I check one, a new row gets created while still correctly binding to the collection.

Thanks in advance!


I don't agree with your statement the one I just checked is now on the next empty row. It is still the same button but there is another row. I think you problem is the check/check event is not firing until the button loses focus (e.g. you go the the textbox). In the binding you need to set UpdateSourceTrigger=PropertyChanged.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜