简体   繁体   English

获取组中所选RadioButton的索引

[英]Getting the index of the selected RadioButton in a group

I have got a reference to a RadioButton rb1. 我有对RadioButton rb1的引用。
How can I get the index of the selected RadioButton in rb1's group ? 如何获取rb1组中所选RadioButton的索引?
I have googled for a while but without success. 我已经搜索了一段时间,但没有成功。

Any help will be greatly appreciated 任何帮助将不胜感激

Well short answer to your question is you don't. 对您的问题的简短回答是,您不会。 What you should do is bind RadioButton.IsChecked to some bool property of your view model. 您应该做的是将RadioButton.IsChecked绑定到视图模型的某些bool属性。 You can achieve something like group index by binding int property of your view model via your implementation of IValueConverter : 您可以通过实现IValueConverter视图模型的int属性来实现类似组索引的功能:

View Model property: 查看模型属性:

private int _groupIndex = 1;

public int GroupIndex
{
   get { return _groupIndex; }
   set
   {
       if (_groupIndex == value) return;
       _groupIndex = value;
       OnPropertyChanged("GroupIndex");
   }
}

Converter: 转换器:

public class IndexBooleanConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
      if (value == null || parameter == null)
          return false;
      else
          return (int)value == System.Convert.ToInt32(parameter);
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
      if (value == null || parameter == null)
          return null;
      else if ((bool)value)
          return System.Convert.ToInt32(parameter);
      else
          return DependencyProperty.UnsetValue;
  }
}   

and then you bind it like this: 然后像这样绑定它:

<StackPanel Orientation="Horizontal">
   <StackPanel.Resources>
       <local:IndexBooleanConverter x:Key="IndexBooleanConverter"/>
   </StackPanel.Resources>
   <RadioButton Content="Option1" IsChecked="{Binding Path=GroupIndex, Converter={StaticResource IndexBooleanConverter}, ConverterParameter=1}"/>
   <RadioButton Content="Option2" IsChecked="{Binding Path=GroupIndex, Converter={StaticResource IndexBooleanConverter}, ConverterParameter=2}"/>
   <RadioButton Content="Option3" IsChecked="{Binding Path=GroupIndex, Converter={StaticResource IndexBooleanConverter}, ConverterParameter=3}"/>
</StackPanel>

In this case your view model property GroupIndex will have values 1, 2 or 3 depending on what RadioButton is ticked 在这种情况下,取决于选中哪个RadioButton ,视图模型属性GroupIndex将具有值1、2或3

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM