打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
RadioButton分组的实现
2010-11-22 11:15 by Clingingboy, 12384 阅读, 1 评论, 收藏, 编辑

 

 

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++;            }        }    }}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java 集合系列08: List总结(LinkedList, ArrayList等使用场景和性能分...
C# 数组 总结
山东创睦网络科技有限公司:手写一个Java爬虫
精解四大集合框架:List核心知识总结
Java 过滤敏感字的算法
获得手机通讯录联系人信息
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服