中获取组的单选按钮

中获取组的单选按钮

本文介绍了在 Silverlight 3 中获取组的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Silverlight 3 应用程序,它有使用 GroupName 属性分组的单选按钮.我想在代码中做的是检索属于指定组的所有单选按钮.有没有一种简单的方法可以做到这一点,或者我是否需要遍历所有控件?

I have a Silverlight 3 application, and it has radiobuttons grouped using the GroupName property. What I would like to do in the code is retrieve all the radiobuttons that are part of a specified group. Is there an easy way to do this, or would I need to iterate over all the controls?

谢谢.

推荐答案

从这个 answer(我真的很需要写博客):-

Borrowing (yet again) my VisualTreeEnumeration from this answer (I really really need to blog):-

public static class VisualTreeEnumeration
{
   public static IEnumerable<DependencyObject> Descendents(this DependencyObject root)
   {
     int count = VisualTreeHelper.GetChildrenCount(root);
     for (int i=0; i < count; i++)
     {
       var child = VisualTreeHelper.GetChild(root, i);
       yield return child;
       foreach (var descendent in Descendents(child))
         yield return descendent;
     }
   }
}

将其放置在您的主命名空间或实用程序命名空间中的文件中,您在代码中将 using 用于该命名空间.

Place this in a file in either your main namespace or in a utility namespace that you place a using for in your code.

现在您可以使用 LINQ 来获取各种有用的列表.在您的情况下:-

Now you can use LINQ to get all sorts of useful lists. In your case:-

 List<RadioButton> group = this.Descendents()
                               .OfType<RadioButton>()
                               .Where(r => r.GroupName == "MyGroupName")
                               .ToList();

这篇关于在 Silverlight 3 中获取组的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:25