【问题标题】:Uncheck all other checkboxes if one is checked如果选中一个,请取消选中所有其他复选框
【发布时间】:2017-01-03 09:24:17
【问题描述】:

我使用了 XAML ListView,Listview 中的一列是复选框。当数据绑定到 ListView 时,它会创建多行,其中一个列作为复选框。我面临的问题是我希望用户只能选择一个复选框,而现在他可以选择多个。当用户选择一个时,所有其他复选框都应该取消选中。

这是 XAML 部分。

<ListView  ScrollViewer.VerticalScrollBarVisibility="Auto"  Name="ClientList" Width="Auto" Height="Auto" BorderThickness="0.5" BorderBrush="#cccccc" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Stretch" Margin="20,10,60,0" >

                    <ListView.ItemContainerStyle>

                        <Style TargetType="ListViewItem">
                            <Setter Property="Focusable" Value="false"/>
                            <Setter Property="Foreground" Value="Black"/>
                            <Setter Property="Height" Value="30"/>
                        </Style>
                    </ListView.ItemContainerStyle>

                    <ListView.View>
                        <GridView  ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyle1}">
                            <GridViewColumn Header="Client Name " x:Name="clienteName" DisplayMemberBinding="{Binding ClientName}"  Width="390"  />
                            <GridViewColumn Header="Client Code " x:Name="ClienteCode" DisplayMemberBinding="{Binding ClientCode}"  Width="195" />
                            <GridViewColumn Header="Select" Width="57">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate >
                                        <Grid >
                                            <CheckBox DataContext="{Binding ClientCode}"  Click="CheckBox_Click" TouchDown="CheckBox_TouchDown"/>
                                        </Grid>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>

                        </GridView>
                    </ListView.View>
</ListView>

用于将数据绑定到 ListView 的 CS

List<Client> list = new List<Client>();
ClientList.Items.Clear();
list = getListFromDT(dt);

foreach (Client pr in list)
{
   ClientList.Items.Add(pr);
}  

【问题讨论】:

  • 听起来你真正想要的是一个 RadioButton...
  • @doubleYou 我希望 CheckBox 的行为类似于 RadioButton,或者我可以使用看起来像 CheckBox 的 RadioButton。

标签: c# .net wpf listview checkbox


【解决方案1】:
  1. 请使用 RadioButton - 它具有所需的行为。如果您希望它看起来像一个复选框,那么将 RadioButtons 样式更改为看起来像一个复选框。
  2. 如果您仍想使用 CheckBox - 那么 foreach(GridView 中的 var item)是您的选择。

【讨论】:

    【解决方案2】:

    你尝试过这样的事情吗?

      void check_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox sendercheck = sender as CheckBox;
            if (sendercheck.Checked)
            {
                foreach (var c in Container.Children)
                {
                    CheckBox check = c as CheckBox;
                    if (check != null)
                    {
                        if (check.Id != sendercheck.Id)
                        {
                            check.Checked = false;
                        }
                    }
                }
            }
        }
    

    这里的容器应该是你的列表

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      相关资源
      最近更新 更多