简体   繁体   中英

Radio Button grouping in the multi column list view

I have a list view with 3 following columns: 1. a Text Column 2. Radio button 3. Radio button

I am trying to group the two radio Button but all the radio button gets in one group. eg if there are two listview items (two rows), then all the 4 radio button gets grouped as one.

<ListView Name="ruleListView"  ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                    ScrollViewer.VerticalScrollBarVisibility="Auto" 
 Margin="10">
                <ListView.View>
                    <GridView>
                        <GridView.ColumnHeaderContainerStyle>
                            <Style TargetType="{x:Type GridViewColumnHeader}">
                                <Setter Property="IsEnabled" Value="False"/>
                            </Style>
                        </GridView.ColumnHeaderContainerStyle>
                        <GridViewColumn 
                                Header="Rule"

                                >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock 
                                        HorizontalAlignment="Right"
                                        Width="410"
                                        TextWrapping="Wrap" Text="{Binding Rule}" TextAlignment="Left"
                                        />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn 
                                Header="Configuration Key"
                            Width="140"

                                >

                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                        <RadioButton GroupName="Rule"
                                                     VerticalAlignment="Center" 
                                                     HorizontalAlignment="Center" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn 
                                Header="Event"
                                Width="100"
                                >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                        <RadioButton GroupName="Rule"
                                                 VerticalAlignment="Center" 
                                                 HorizontalAlignment="Center" 
                                                 IsChecked="True"
                                                 />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>

I expect that grouping should be done as per the particular row.

As I understand it, the RadioButton.GroupName applies to all active controls. That is why you are seeing all the radio buttons linked together. In order to get each row to be linked you need a unique GroupName for each row. In MVVM you could bind the GroupName to a property on the underlying item, or bind the RadioButton's IsChecked property to the ViewModel and manage the switching in code.

Since your not using ViewModels it is a little more complicated. You still need to create a unique GroupName per row. The easiest way I can think of to do this is to use a IValueConverter to get the row index.

public class RadioGroupIndexConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is ListViewItem lvi)
        {
            var listView = ItemsControl.ItemsControlFromItemContainer(lvi);
            var index = listView.ItemContainerGenerator.IndexFromContainer(lvi);
            return string.Format("{0}{1}", parameter, index);
        }
        return parameter;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Then you'll need to bind to the GroupName to the ListViewItem using the converter.

First you need to add the converter as a resources in xaml. Where <converters: is a reference to the namespace of the RadioGroupIndexConverter class. If you created the class in the same namespace as your ListView you can just use <local:

<ListView.Resources>
    <converters:RadioGroupIndexConverter x:Key="GroupConverter"/>
</ListView.Resources>

Then you'll change your RadioButton.GroupName to use a relative binding.

<RadioButton GroupName="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Converter={StaticResource GroupConverter}}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM