Getting the index of the selected RadioButton in a group(获取组中选定 RadioButton 的索引)
问题描述
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.
Any help will be greatly appreciated
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 的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取组中选定 RadioButton 的索引
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01