【问题标题】:Getting the index of the selected RadioButton in a group获取组中选定 RadioButton 的索引
【发布时间】:2013-06-09 13:43:38
【问题描述】:

我有一个 RadioButton rb1 的引用。
如何获取 rb1 组中选定 RadioButton 的索引?
我用谷歌搜索了一段时间,但没有成功。

任何帮助将不胜感激

【问题讨论】:

  • 你想达到什么目的?我无法想象你需要这个的用例。

标签: c# wpf radio-button visualtreehelper


【解决方案1】:

你的问题的简短回答是你不知道。您应该做的是将RadioButton.IsChecked 绑定到您的视图模型的一些boolproperty。您可以通过 IValueConverter 的实现绑定视图模型的 int 属性来实现类似组索引的功能:

查看模型属性:

private int _groupIndex = 1;

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

转换器:

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

然后你像这样绑定它:

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

在这种情况下,您的视图模型属性 GroupIndex 将具有值 1、2 或 3,具体取决于选中的 RadioButton

【讨论】:

    【解决方案2】:

    如果您到了这一步,那么您的设计可能有问题,您应该重新考虑。

    话虽如此,您可以遍历可视化树并像这样找到它:

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

    然后遍历孩子并找到选中的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;
        }
    

    【讨论】:

      猜你喜欢
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多