2013-08-26 63 views
0

我無法在我的用戶控件中設置默認的SelectedIndex值。將默認值設置爲動態綁定生成的控件

I`ve創建此的DependencyProperty用於控制(一個單選按鈕組,取作爲一個的ItemSource詞典(鍵:字符串值:位圖)並創建單選按鈕和相應的圖像)。

public int SelectedIndex 
{ 
    get { return (int)GetValue(SelectedIndexProperty); } 
    set { SetValue(SelectedIndexProperty, value); } 
} 
public static readonly DependencyProperty SelectedIndexProperty = 
    DependencyProperty.Register(
           "SelectedIndex", 
           typeof(int), 
           typeof(RadioGroup), 
           new FrameworkPropertyMetadata(0, OnSelectedIndexChanged) 
           ); 
private static void OnSelectedIndexChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) 
{ 
    var control = (RadioGroup)source; 
    if (control.ItemsSource == null) return; // When assigning SelectedIndex from the XAML the collection it`s not initialized yet 
    if (e.NewValue != null) 
    { 
     var radioButton =(RadioButton)VisualTreeHelper.GetChild((control.container.ItemContainerGenerator.ContainerFromIndex((int) e.NewValue)), 0); 
     radioButton.IsChecked = true; 
    } 
    var x = -1; 
    foreach (var item in control.ItemsSource)// Apparently I cannot convert the IEnumerable to List for this i do this odd loop with the external counter 
    { 
     x++; 
     var radioButton = (RadioButton)VisualTreeHelper.GetChild((control.container.ItemContainerGenerator.ContainerFromIndex(x)), 0); 
     if (radioButton.IsChecked != true) continue; 
     control.SelectedIndex = x; 
     var key = control.container.Items[x].ToString(); 
     var currItem = (KeyValuePair<string, Bitmap>)item; 
     control.GroupIcon = currItem.Value; 
     return; 
    } 
} 

這是XAML

<UserControl.Resources> 
    <local:DictToStringConverter x:Key="DictToStringConverter"/> 
    <DataTemplate DataType="{x:Type ItemsPanelTemplate}" x:Key="ItemPanelDatatemplate"> 
     <RadioButton Margin="0,0,5,0" Content="{Binding}" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}, Path=Tag}" Checked="ToggleButton_OnChecked" /> 
    </DataTemplate> 
    <ItemsPanelTemplate x:Key="ItemsPanelTemplate"> 
     <UniformGrid x:Name="grdLayout" Columns="{Binding Path=Columns, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:RadioGroup}}}"/> 
    </ItemsPanelTemplate> 

</UserControl.Resources> 

<Border x:Name="brdMain"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <TextBlock x:Name="txtTitle" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Text=""/> 
     <Image x:Name="imgImage" Grid.Column="0" Grid.Row="1" Source="{Binding Path=GroupIcon, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:RadioGroup}}}" VerticalAlignment="Top"/> 
     <ItemsControl Grid.Column="1" Grid.Row="1" Name="container" Width="{Binding ElementName=scroll, Path=ViewportWidth}" 
        ItemsSource="{Binding Path=ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:RadioGroup}},Converter={StaticResource DictToStringConverter}}" 
        ItemTemplate="{StaticResource ItemPanelDatatemplate}" 
        ItemsPanel="{StaticResource ItemsPanelTemplate}" 
     /> 
    </Grid> 
</Border> 

如果我點擊一個單選按鈕,或者如果我從代碼分配財產的背後它的工作原理(也圖標正常開關), 的問題是,在我分配的XAML SelectedIndex但顯然綁定發生賦值,所以默認情況下沒有選擇RadioButton。

<cust:RadioGroup GroupTitle="Symmetry Axis" ItemsSource="{Binding SymmetryAxis}" Columns="3" ItemSelected="RadioGroup_OnSelected" SelectedIndex="2"/> 

我該如何解決這個問題?

回答

0

好,我設法解決它:

public RadioGroup() 
    { 
     InitializeComponent(); 
     Loaded += RadioGroup_Loaded; 
    } 

void RadioGroup_Loaded(object sender, RoutedEventArgs e) 
{ 
    ((RadioButton)VisualTreeHelper.GetChild((container.ItemContainerGenerator.ContainerFromIndex(SelectedIndex)), 0)).IsChecked = true; 
}