WPF RadioButton的探究,为啥选中一个其他都自动不选中了呢?
研究这个是出于一个群聊中碰到的一个问题:“我现在遇到个很尴尬的局面。。 我封装了一个控件 有状态A和B, 并排放置10个控件在一个Grid下面 希望点击一个控件变为A状态后 其它9个都变为状态B”,这不禁让我想起了WPF的RadioButton, 他有一个有趣的功能:“如果需要用自定义的方法对RadioButton作分组,那么可以用它的GroupName属性,这个属性是字符串类型的,任何拥有相同GroupName 的RadioButton 会被分在同个组里(只要它们在逻辑上属于同一个源)。”
那就用Reflector看看他是怎么实现的吧:
一旦RadioButton的GroupName属性发生变化,他先把自己从原始分组里面注销,然后注册到新的分组:
private static void OnGroupNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { RadioButton instance = (RadioButton) d; string newValue = e.NewValue as string; string str2 = _currentlyRegisteredGroupName.GetValue(instance); if (newValue != str2) { if (!string.IsNullOrEmpty(str2)) { Unregister(str2, instance); } if (!string.IsNullOrEmpty(newValue)) { Register(newValue, instance); } } }
可以看到_groupNameToElement是一个静态哈希表,而且是ThreadStatic的,TreadStaict没记错的话,应该是通过ThreadSlot技术实现的,照理说可以同步所有线程上的RadioButton,但是看后面的代码只会同步同一棵视觉树的,那就搞不懂为什么要这么设计了,难道同一棵视觉树的树叶可以来自于不同线程?:
[Localizability(LocalizationCategory.RadioButton)] public class RadioButton : ToggleButton { // Fields private static readonly UncommonField<string> _currentlyRegisteredGroupName; private static DependencyObjectType _dType; [ThreadStatic] private static Hashtable _groupNameToElements; public static readonly DependencyProperty GroupNameProperty;
那么当一个RadioButton状态改变(UnCheck-->Check)发生了什么?
protected override void OnChecked(RoutedEventArgs e) { this.UpdateRadioButtonGroup(); base.OnChecked(e); } private void UpdateRadioButtonGroup() { string groupName = this.GroupName; if (!string.IsNullOrEmpty(groupName)) { Visual visualRoot = KeyboardNavigation.GetVisualRoot(this); if (_groupNameToElements == null) { _groupNameToElements = new Hashtable(1); } lock (_groupNameToElements) { ArrayList list = (ArrayList) _groupNameToElements[groupName]; int index = 0; while (index < list.Count) { WeakReference reference = (WeakReference) list[index]; RadioButton target = reference.Target as RadioButton; if (target == null) { list.RemoveAt(index); } else { if ((target != this) && ((target.IsChecked == true) && (visualRoot == KeyboardNavigation.GetVisualRoot(target)))) { target.UncheckRadioButton(); } index++; } } return; } } DependencyObject parent = base.Parent; if (parent != null) { IEnumerator enumerator = LogicalTreeHelper.GetChildren(parent).GetEnumerator(); while (enumerator.MoveNext()) { RadioButton current = enumerator.Current as RadioButton; if ((((current != null) && (current != this)) && string.IsNullOrEmpty(current.GroupName)) && (current.IsChecked == true)) { current.UncheckRadioButton(); } } } }
看到没?他找到VisualRoot下所有的RadioButton,把他们都Uncheck了,而且他只会同步同一棵视觉树(VisualTree),注意到,他使用了弱引用,而且检测了“死亡”了的RadioButton。这是因为RadioButton析构时,不会调用注销函数(其实,RadioButton不是Disposable的)。最后,为了完整性,再来看看注册、注销、移除死亡按钮的几个函数吧:
private 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)); } _currentlyRegisteredGroupName.SetValue(radioButton, groupName); } private 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); } } } _currentlyRegisteredGroupName.SetValue(radioButton, null); } } 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++; } } } private void UncheckRadioButton() { base.ClearValue(ToggleButton.IsCheckedProperty); if (base.IsChecked == true) { bool flag; BaseValueSourceInternal internal2 = base.GetValueSource(ToggleButton.IsCheckedProperty, null, out flag); if (((internal2 != BaseValueSourceInternal.ThemeStyleTrigger) && (internal2 != BaseValueSourceInternal.TemplateTrigger)) && (internal2 != BaseValueSourceInternal.ParentTemplateTrigger)) { base.IsChecked = false; } } }