Getting the index of the selected RadioButton in a group

瘦欲@ 提交于 2019-11-29 12:30:13

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

Well short answer to your question is you don't. What you should do is bind RadioButton.IsChecked to some boolproperty of your view model. You can achieve something like group index by binding int property of your view model via your implementation of IValueConverter:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!