/// <summary> /// 更新关联的radiobutton /// </summary> private void UpdateRadioButtonGroup() { string groupName = GroupName; if (!string.IsNullOrEmpty(groupName)) { Visual rootScope = GetVisualRoot(this); if (_groupNameToElements == null) { _groupNameToElements = new Hashtable(1); } lock (_groupNameToElements) { ArrayList elements = (ArrayList)_groupNameToElements[groupName]; for (int i = 0; i < elements.Count;) { WeakReference weakReference = (WeakReference)elements[i]; FIconRadioButton rb = weakReference.Target as FIconRadioButton; if (rb == null) { elements.RemoveAt(i); } else { if (rb != this && (rb.IsChecked == true) && rootScope == GetVisualRoot(rb)) { rb.UncheckRadioButton(); } i++; } } } } else { DependencyObject parent = this.Parent; if (parent != null) { IEnumerable children = LogicalTreeHelper.GetChildren(parent); IEnumerator itor = children.GetEnumerator(); while (itor.MoveNext()) { FIconRadioButton rb = itor.Current as FIconRadioButton; if (rb != null && rb != this && string.IsNullOrEmpty(rb.GroupName) && (rb.IsChecked == true)) { rb.UncheckRadioButton(); } } } } }
/// <summary> /// GroupName的变更回调 /// </summary> private static void OnGroupNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { FIconRadioButton radioButton = (FIconRadioButton)d; string groupName = e.NewValue as string; string currentlyRegisteredGroupName = (string)radioButton.GetValue(_currentlyRegisteredGroupName); if (groupName != currentlyRegisteredGroupName) { if (!string.IsNullOrEmpty(currentlyRegisteredGroupName)) { Unregister(currentlyRegisteredGroupName, radioButton); } if (!string.IsNullOrEmpty(groupName)) { Register(groupName, radioButton); } } }
/// <summary> /// 注销一对RadioButton与GroupName的关联 /// </summary> private static void Unregister(string groupName, FIconRadioButton radioButton) { if (_groupNameToElements == null) { return; } lock (_groupNameToElements) { ArrayList elements = (ArrayList)_groupNameToElements[groupName]; if (elements != null) { PurgeDead(elements, radioButton); if (elements.Count == 0) { _groupNameToElements.Remove(groupName); } } } radioButton.SetValue(_currentlyRegisteredGroupName, null); }
/// <summary> /// 注册一对RadioButton与GroupName的关联 /// </summary> private static void Register(string groupName, FIconRadioButton 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)); } radioButton.SetValue(_currentlyRegisteredGroupName, groupName); }