wpfdatagrid radiobutton选择和取消选择问题

时间:2015-08-19 07:26:20

标签: c# wpf xaml datagrid wpfdatagrid

我得到了一个数据网格,每行有几列,只有radiobutton。实际上,我遇到了两个问题:

  1. 我可以选择每行多个radiobutton。那不好,我必须只选一个。
  2. 选择单选按钮时,我无法取消选择。
  3. 我只有XAML代码:

    <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}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Légère" Width="60">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="{Binding XPath=ID}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Modérée" Width="60">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="{Binding XPath=ID}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Forte" Width="60">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="{Binding XPath=ID}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Totale" Width="60">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="{Binding XPath=ID}" />
                            </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>
    

1 个答案:

答案 0 :(得分:1)

假设该属性为string CurrentOption

添加此

<UserControl.Resources>
    <self:CurrentOptionConverter x:Key="CurrentOptionConverter" />
</UserControl.Resources>

更改为此,对于问题1,通过ID解决此问题应为字符串。

<DataGrid AutoGenerateColumns="False" VerticalAlignment="Stretch"  Grid.Row="1" Name="dg1" ItemsSource="{Binding Source={StaticResource XmlData},XPath=Limits/*}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Capacite" CellTemplate="{StaticResource CustomCapacityTemplate}" />
        <DataGridTemplateColumn Header="Clear" Width="60">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding XPath=ID}" 
                        IsChecked={Binding XPath=CurrentOption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                        Converter={StaticResource CurrentOptionConverter}, ConverterParameter={x:Null}}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Aucune" Width="60">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding XPath=ID}" 
                        IsChecked={Binding XPath=CurrentOption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                        Converter={StaticResource CurrentOptionConverter}, ConverterParameter=Aucune}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Legere" Width="60">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding XPath=ID}" 
                        IsChecked={Binding XPath=CurrentOption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                        Converter={StaticResource CurrentOptionConverter}, ConverterParameter=Legere}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Moderee" Width="60">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding XPath=ID}" 
                        IsChecked={Binding XPath=CurrentOption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                        Converter={StaticResource CurrentOptionConverter}, ConverterParameter=Moderee}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Forte" Width="60">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding XPath=ID}" 
                        IsChecked={Binding XPath=CurrentOption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                        Converter={StaticResource CurrentOptionConverter}, ConverterParameter=Forte}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Totale" Width="60">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <RadioButton GroupName="{Binding XPath=ID}" 
                        IsChecked={Binding XPath=CurrentOption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
                        Converter={StaticResource CurrentOptionConverter}, ConverterParameter=Totale}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>

<强> CurrentOptionConverter.cs

public class CurrentOptionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null && parameter != null)
        {
            return value.Equals(parameter);
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((bool)value)
        {
            return parameter;
        }
        return Binding.DoNothing;
    }
}