2013-06-13 144 views
1

我已經獲得了一個RadioButton rb1的引用。
如何獲取rb1組中所選RadioButton的索引?
我已經搜索了一段時間,但沒有成功。獲取組中所選RadioButton的索引

任何幫助,將不勝感激

+2

您試圖實現什麼?我無法想象你需要這個用例。 – WiiMaxx

回答

0

如果你走到這一步那麼有可能出錯了你的設計,你應該重新考慮它。

這樣說,你可以遍歷可視化樹,並發現它是這樣的:

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

然後去了孩子,發現被檢查的單選按鈕

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

好簡短的回答你的問題是你沒有。你應該做的是將RadioButton.IsChecked綁定到你的視圖模型的一些bool屬性。

查看模型屬性::

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

,然後你把它綁定您可以通過您的落實IValueConverter結合您的視圖模型的int財產實現類似組索引像這樣:

<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被勾選的內容