【问题标题】:multiple radiobuttons selection in datagrid数据网格中的多个单选按钮选择
【发布时间】:2015-03-02 20:29:29
【问题描述】:

我有一个包含四列(一列有标签,三列是单选按钮类型)和多行的数据网格。我必须确保用户必须在每一行中选择一个单选按钮。

单选按钮的列定义是这样的

<telerik:GridViewColumn Header="Different" HeaderTextAlignment="Center" UniqueName="diff" Width="100">
  <telerik:GridViewColumn.CellTemplate>
    <DataTemplate>
     <telerik:RadRadioButton Name="rdbtnDiff" GroupName="DiffTariff" Width="15" Height="15"/>

     </DataTemplate>
    </telerik:GridViewColumn.CellTemplate>
   </telerik:GridViewColumn>

如果我为所有单选按钮类型列提供相同的 GroupName,它允许我在整个网格中只选择一个单选按钮。当我为所有三列提供不同的 GroupName 时,它允许我在整列中只选择一个单选按钮。但我想排成一排。

我完全被困住了。任何形式的帮助将不胜感激。

【问题讨论】:

  • 最终,您需要确保每行的组名都是唯一的,因此您必须将其绑定到某些东西,例如"DiffTariff + SomeIdentifier"
  • 您可以为每一行创建不同的组。
  • @BenRobinson 我如何在 xaml 中做到这一点?
  • @JawadZeb 我知道我必须这样做,但我不明白如何在 xaml 中做到这一点?
  • @NailaAkbar 检查我已经创建了一个简单的示例,将它重构为您的要求。

标签: c# silverlight datagrid radio-button


【解决方案1】:

这是我已经测试并按要求工作的代码。

<Grid>
    <Grid.ColumnDefinitions>



    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="150" ></RowDefinition>
        <RowDefinition Height="150"></RowDefinition>

    </Grid.RowDefinitions>




    <StackPanel Orientation="Horizontal" Margin="0" Grid.Row="0">

        <RadioButton GroupName="row1group">Btn1</RadioButton>
        <RadioButton GroupName="row1group">Btn2</RadioButton>
        <RadioButton GroupName="row1group">Btn3</RadioButton>

    </StackPanel>


    <StackPanel Orientation="Horizontal" Margin="0" Grid.Row="1" >


        <RadioButton GroupName="row2group">Btn1</RadioButton>
        <RadioButton GroupName="row2group">Btn2</RadioButton>
        <RadioButton GroupName="row2group">Btn3</RadioButton>
    </StackPanel>
</Grid>

【讨论】:

  • 但我有动态行数。
  • 您可以使用 java script 添加它们。请添加一些我将对其进行更改的代码。
  • 不,我必须在没有任何第三方的情况下这样做。我找到了解决方案。谢谢
【解决方案2】:

需要注意的主要一点是,我们应该有一个在整个集合中保持唯一的属性。如果我们有这样的属性,只需将它与组名绑定,单选按钮就可以正常工作。它可以是 index 属性或任何其他属性。

所以我通过为每一行指定一个唯一的组名来解决这个问题。

This Helped me to do this.

这里是示例代码;

<dg:DataGridTemplateColumn Header="Male">                    
    <dg:DataGridTemplateColumn.CellTemplate>                        
        <DataTemplate>                            
            <RadioButton GroupName="{Binding Index}" IsChecked="{Binding Male}" />                        
        </DataTemplate>                    
    </dg:DataGridTemplateColumn.CellTemplate>                
</dg:DataGridTemplateColumn>                
<dg:DataGridTemplateColumn Header="Female">                    
    <dg:DataGridTemplateColumn.CellTemplate>                        
        <DataTemplate>                            
            <RadioButton GroupName="{Binding Index}" IsChecked="{Binding Female}" />                        
        </DataTemplate>                    
    </dg:DataGridTemplateColumn.CellTemplate>                
</dg:DataGridTemplateColumn>

【讨论】:

  • 感谢您的更正。我编辑了我的答案。现在还好吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 2018-10-02
  • 2016-05-28
  • 1970-01-01
相关资源
最近更新 更多