获得所选RadioButton的指数在一组 [英] Getting the index of the selected RadioButton in a group
本文介绍了获得所选RadioButton的指数在一组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个单选按钮RB1。结果
我怎样才能获得所选择的单选按钮的索引RB1的组的参考?结果
我用Google搜索了一会儿,但没有成功
I have got a reference to a RadioButton rb1.
How can I get the index of the selected RadioButton in rb1's group ?
I have googled for a while but without success.
任何帮助将不胜感激。
推荐答案
如果你有,这点则有可能出错了你的设计,你应该重新考虑它
If you got to this point then there is probably something wrong with your design and you should reconsider it.
这是说,你可以遍历可视化树,并发现它是这样的:
that being said you could traverse the visual tree and find it like this:
/// Returns the first GroupBox ancester
public DependencyObject FindAncestor(DependencyObject current)
{
current = VisualTreeHelper.GetParent(current);
while (current != null)
{
if (current is GroupBox)
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
};
return null;
}
然后再在儿童和发现检查单选按钮
then go over the children and find the checked radioButton
public RadioButton FindChild<T>(DependencyObject parent)
{
// Confirm parent and childName are valid.
if (parent == null) return null;
RadioButton foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
var childType = child as radioButton;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild(child);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) return foundChild ;
}
else if (childName.IsChecked == true)
{
return foundChild;
}
}
return null;
}
这篇关于获得所选RadioButton的指数在一组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文