Search code examples
c#wpfxamldatatemplate

Two groups for Radio Button in a DataTemplate?


Hello I have the following Problem: I have a DataTemplate with RadioButtons and I want that each RadioButton functionally belongs to the two groups. Is this possible?

So I want that I can only click one Button in a row and one button in a column.

At the moment I can click a button twice in a column, but I can click every button in 1 row.

I found nothing helpful on Google. Anyone got an Idea?

                   <GridViewColumn Width="80">
                    <GridViewColumn.Header>
                        <GridViewColumnHeader>Hauptfoto</GridViewColumnHeader>
                    </GridViewColumn.Header>
                    <GridViewColumn.CellTemplate >
                        <DataTemplate>
                            <RadioButton Foreground="DarkBlue" GroupName="HauptfotoRB" x:Name="HauptfotoRB" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="25,0,0,0" Checked="HauptfotoRB_Checked"></RadioButton>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="80">
                    <GridViewColumn.Header>
                        <GridViewColumnHeader>Oben__1</GridViewColumnHeader>
                    </GridViewColumn.Header>
                    <GridViewColumn.CellTemplate >
                        <DataTemplate>
                            <RadioButton Foreground="DarkBlue" GroupName="ObenRB" x:Name="ObenRB" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="25,0,0,0" Checked="ObenRB_Checked" ></RadioButton>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="80">
                    <GridViewColumn.Header>
                        <GridViewColumnHeader>Innen__2</GridViewColumnHeader>
                    </GridViewColumn.Header>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <RadioButton Foreground="DarkBlue" GroupName="InnenRB"  x:Name="InnenRB"  VerticalAlignment="Center" HorizontalAlignment="Center" Margin="25,0,0,0" Checked="InnenRB_Checked" ></RadioButton>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

Solution

  • For a DataGrid you could e.g. use second invisible RadioButton for the columns synchronization and AlternationIndex as row index for the row synchronization (if you have an ID by your row data, than of course it can be used instead of AlternationIndex for row synchronization) and do bind them with each other:

    <DataGrid ItemsSource="{Binding YourCollection}" AutoGenerateColumns="False" AlternationCount="2147483647">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <RadioButton GroupName="col1" IsChecked="{Binding IsChecked, ElementName=b}" Visibility="Collapsed"/>
                            <RadioButton Name="b" 
                                            Content="SomeContent" 
                                            GroupName="{Binding AlternationIndex, RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <RadioButton GroupName="col2" IsChecked="{Binding IsChecked, ElementName=b}" Visibility="Collapsed"/>
                            <RadioButton Name="b" 
                                            Content="SomeContent" 
                                            GroupName="{Binding AlternationIndex, RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <RadioButton GroupName="col3" IsChecked="{Binding IsChecked, ElementName=b}" Visibility="Collapsed"/>
                            <RadioButton Name="b" 
                                            Content="SomeContent" 
                                            GroupName="{Binding AlternationIndex, RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>