原文:RadioButton分组的实现

 

 

XAML如下

    <StackPanel>
<RadioButton GroupName="colorgrp">Red</RadioButton>
<RadioButton GroupName="colorgrp">Blue</RadioButton>
<RadioButton GroupName="numgrp">1</RadioButton>
<RadioButton GroupName="numgrp">2</RadioButton>
<RadioButton>4</RadioButton>
<RadioButton>5</RadioButton>
</StackPanel>

在一个Panel下,若没有指定GroupName则为一组,指定了GroupName为另外一组.

逻辑:一组内,选中一个Button取消其他的Button选中状态.

在没有分组状态下

即把Button的父容器找出来,然后反选未指定GroupName的Button

分组概念

要把多个Button存为一组,即多个key,每个key对应一个列表,可以以HashTable为基础.

应该由内部控件来调用,否则可能会引起重复的问题

public class RadioGroup
{
[ThreadStatic]
private static Hashtable _groupNameToElements; public static void Register(string groupName, RadioButton radioButton)
{
if (_groupNameToElements == null)
{
_groupNameToElements = new Hashtable(1);
}
lock (_groupNameToElements)
{
ArrayList elements = (ArrayList)_groupNameToElements[groupName];
if (elements == null)
{
elements = new ArrayList(1);
_groupNameToElements[groupName] = elements;
}
else
{
PurgeDead(elements, null);
}
elements.Add(new WeakReference(radioButton));
}
} public static void Unregister(string groupName, RadioButton radioButton)
{
if (_groupNameToElements != null)
{
lock (_groupNameToElements)
{
ArrayList elements = (ArrayList)_groupNameToElements[groupName];
if (elements != null)
{
PurgeDead(elements, radioButton);
if (elements.Count == 0)
{
_groupNameToElements.Remove(groupName);
}
}
}
}
} private static void PurgeDead(ArrayList elements, object elementToRemove)
{
int index = 0;
while (index < elements.Count)
{
WeakReference reference = (WeakReference)elements[index];
object target = reference.Target;
if ((target == null) || (target == elementToRemove))
{
elements.RemoveAt(index);
}
else
{
index++;
}
}
}
}

RadioButton分组的实现的更多相关文章

  1. PyQt中对RadioButton分组

    我们知道在同一widget组件中的radio button有排他属性,为了在一个窗口中显示多组radio button,我们需要对其分组. 通常有两种选择,一种是BoxGroup,另一种是Button ...

  2. VC++中如何让RadioButton分组,并且互斥

    首先让两个RadioButton的Group属性为False. 将两个RadioButton放入一个GroupBox里面,即可如果分成多个组,则将多组Radio分别用GroupBox框起来,并将每组的 ...

  3. 常用的winform属性和工具箱工具

    WinForm: 客户端程序制作 - C/S B/S(外部端,网页端) 不是必须在windows系统上才能运行是在.NET Framework框架上运行  ,在苹果系统上有框架照样可以运行 常用窗体属 ...

  4. 初识windows程序需要了解的知识点

    初识一件事物我们会有陌生,我们慢慢地去了解它就会懂,让我带你们一起了解吧. 一.Form是.Net Framework 定义好的一个最基本的窗体类,具有窗体基本属性和方法 属性            ...

  5. winform 属性

    WinForm为客户端程序必须在.NET Framework框架上运行 一.常用属性: 布局: AutoScroll:当控件内容超出可见区域是否显示滚动条: Autosize:当控件内容有超出时是否自 ...

  6. WinFom基本属性

    常用窗体属性: AutoSroll:如果窗体内容超出了窗体范围,自动出现滚动条.默认是false. AutoSize:调节窗口大小时,保证使窗体内的内容必须全部显示出来. Location:控件左上角 ...

  7. winform属性

    WinForm为客户端程序必须在.NET Framework框架上运行 一.常用属性: 布局: AutoScroll:当控件内容超出可见区域是否显示滚动条: Autosize:当控件内容有超出时是否自 ...

  8. 【winform】userControl刷新父窗体的datagridview

    1.ContextMenuStrip 获取右键控件名称 this.contextMenuScriptScore.SourceControl.Name; //当前控件名 2.radiobutton 分组 ...

  9. Winform 各种属性、方法、控件

    窗体是程序与用户交互的可视界面,窗体也是对象,窗体类定义了生成窗体的模版,实例化一个窗体类就产生了一个窗体. .NET框架类库的System.Windows.Forms命名空间中定义的Form类是所有 ...

随机推荐

  1. EL表达式.md

    操作符 描述 . 访问一个Bean属性或者一个映射条目 [] 访问一个数组或者链表的元素 ( ) 组织一个子表达式以改变优先级 + 加 - 减或负 * 乘 / or div 除 % or mod 取模 ...

  2. 实现indexOf

    1.先判断Array数组是否含有indexOf方法,如果有直接返回结果:如果没有则利用循环比较得到结果. function indexOf(arr, item) { if(Array.prototyp ...

  3. 【AtCoder ABC 075 A】One out of Three

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map轻松搞定 [代码] #include <bits/stdc++.h> using namespace std; ...

  4. SpringBoot日志logback-spring.xml分环境(转)

    springboot按照profile进行打印日志 log4j logback slf4j区别? 首先谈到日志,我们可能听过log4j logback slf4j这三个名词,那么它们之间的关系是怎么样 ...

  5. FPGA实现UHS的一些资料

    对使用FPGA和SD卡进行UHS模式通信的评估: 论文:基于FPGA的SD UHS-II卡控制器设计与实现 设计IP:SD UHS-II Host Controller 供应商: System Lev ...

  6. ARCGIS动态画点

    小马哥淡定 原文 ARCGIS动态画点 private void DrawPointOnMap(double x, double y,bool clear) { IMapControl2 pMapCt ...

  7. html始终让元素居中显示,背景图铺满随便拖动不出界

    首先.写两个class属性 body { margin: 0; padding: 0; height: 100%; width: 100%; background-image: url(../Cont ...

  8. 8、摄像头驱动_Linux的V4L2架构分析

    V4L2架构可以参考  linux-3.4.2\Documentation\video4linux\v4l2-framework.txt V4L2全名为Video For Linux 2,它是针对Li ...

  9. Need ffmpeg exe. You can download it by calling: imageio.plugins.ffmpeg.download()

    该问题 Need ffmpeg exe. You can download it by calling: imageio.plugins.ffmpeg.download()往往出现在在调用 impor ...

  10. ASP.NET MVC中实现多个button提交的几种方法

    有时候会遇到这样的情况:在一个表单上须要多个button来完毕不同的功能,比方一个简单的审批功能. 假设是用webform那不须要讨论,但asp.net mvc中一个表单仅仅能提交到一个Action处 ...