Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I got a datagrid with few column per row with radiobutton. Actually, I got 2 problems:

I can select multiple radiobutton per row. That's not good, I must select only one.
I can't unselect when a radiobutton's selected.

Not working :(

I followed this tutorial: http://wpftutorial.net/RadioButton.html[^]

My code:
XML
<Window.Resources>
        <XmlDataProvider x:Key="XmlData" Source="limits.xml"/>
        <DataTemplate x:Key="CustomCapacityTemplate">
            <StackPanel>
                <TextBlock Text="{Binding XPath=title}" FontWeight="Bold" Margin="10" />
                <TextBlock Text="{Binding XPath=description}" Margin="10 0" />
            </StackPanel>
        </DataTemplate>
        <local:EnumMatchToBooleanConverter x:Key="enumConverter" />
    </Window.Resources>
...
<DataGrid AutoGenerateColumns="False" VerticalAlignment="Stretch"  Grid.Row="1" Name="dg1" ItemsSource="{Binding Source={StaticResource XmlData},XPath=Limits/*}">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Capacité" CellTemplate="{StaticResource CustomCapacityTemplate}" />
                    <DataGridTemplateColumn Header="Aucune" Width="60">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="{Binding XPath=ID}" IsChecked="{Binding Path=CurrentOption, Mode=TwoWay, Converter={StaticResource enumConverter}, ConverterParameter=Aucune}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Légère" Width="60">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="{Binding XPath=ID}" IsChecked="{Binding Path=CurrentOption, Mode=TwoWay, Converter={StaticResource enumConverter}, ConverterParameter=Légère}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Modérée" Width="60">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="{Binding XPath=ID}" IsChecked="{Binding Path=CurrentOption, Mode=TwoWay, Converter={StaticResource enumConverter}, ConverterParameter=Modérée}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Forte" Width="60">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="{Binding XPath=ID}" IsChecked="{Binding Path=CurrentOption, Mode=TwoWay, Converter={StaticResource enumConverter}, ConverterParameter=Forte}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Totale" Width="60">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="{Binding XPath=ID}" IsChecked="{Binding Path=CurrentOption, Mode=TwoWay, Converter={StaticResource enumConverter}, ConverterParameter=Totale}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
                
                <DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding XPath=explicationTitle}" FontWeight="Bold" Margin="10" />
                            <TextBlock Text="{Binding XPath=explicationDescription}" Margin="10" />
                            <TextBlock Text="Aucune" FontWeight="Bold" Margin="10" />
                            <TextBlock Text="{Binding XPath=explicationLimiteAucune}" Margin="10" />
                            <TextBlock Text="Modérée" FontWeight="Bold" Margin="10" />
                            <TextBlock Text="{Binding XPath=explicationLimiteModeree}" Margin="10" />
                            <TextBlock Text="Totale" FontWeight="Bold" Margin="10" />
                            <TextBlock Text="{Binding XPath=explicationLimiteTotale}" Margin="10" />
                        </StackPanel>
                    </DataTemplate>
                </DataGrid.RowDetailsTemplate>
            </DataGrid>


C#
using System;
using System.Globalization;
using System.Windows.Data;

namespace ICF_Form
{
    class EnumMatchToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
                              object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return false;

            string checkValue = value.ToString();
            string targetValue = parameter.ToString();
            return checkValue.Equals(targetValue,
                     StringComparison.InvariantCultureIgnoreCase);
        }

        public object ConvertBack(object value, Type targetType,
                                  object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return null;

            bool useValue = (bool)value;
            string targetValue = parameter.ToString();
            if (useValue)
                return Enum.Parse(targetType, targetValue);

            return null;
        }
    }
}
Posted
Updated 18-Aug-15 23:12pm
v2
Comments
Kevin Marois 19-Aug-15 16:24pm    
It looks like your radio buttons isChecked are all bound to the same property. If so, then checking one will cause all the others to be checked. Also, check your GroupName. Put a breakpoint in your converter and see what values are passed in and what is returned. If you can check more than one, then the group name is probably not what you think it is

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900