C# 设置默认值以动态绑定生成的控件

C# 设置默认值以动态绑定生成的控件,c#,wpf,xaml,C#,Wpf,Xaml,我无法在用户控件中设置默认的SelectedIndex值 我已经为控件(一个RadioButton组,它将一个字典(key:string-value:Bitmap)作为ItemSource并创建RadioButton和相应的图像)创建了这个DependencyProperty public int SelectedIndex { 获取{return(int)GetValue(SelectedIndexProperty);} set{SetValue(SelectedIndexProperty,v

我无法在用户控件中设置默认的SelectedIndex值

我已经为控件(一个RadioButton组,它将一个字典(key:string-value:Bitmap)作为ItemSource并创建RadioButton和相应的图像)创建了这个DependencyProperty

public int SelectedIndex
{
获取{return(int)GetValue(SelectedIndexProperty);}
set{SetValue(SelectedIndexProperty,value);}
}
public static readonly dependencProperty SelectedIndexProperty=
从属属性。寄存器(
“SelectedIndex”,
类型(int),
类型(放射组),
新的FrameworkPropertyMetadata(0,OnSelectedIndexChanged)
);
SelectedIndexChanged上的私有静态无效(DependencyObject源、DependencyPropertyChangedEventArgs e)
{
变量控制=(放射组)源;
if(control.ItemsSource==null)return;//从XAML中为SelectedIndex分配集合时,该集合尚未初始化
如果(如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)//显然,我无法将IEnumerable转换为List,为此,我使用外部计数器执行这个奇数循环
{
x++;
var radioButton=(radioButton)VisualTreeHelper.GetChild((control.container.ItemContainerGenerator.ContainerFromIndex(x)),0);
如果(radioButton.IsChecked!=true)继续;
control.SelectedIndex=x;
var key=control.container.Items[x].ToString();
var currItem=(KeyValuePair)项;
control.GroupIcon=currItem.Value;
返回;
}
}
这是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,但显然绑定发生在分配之后,因此默认情况下没有选择单选按钮

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


如何修复此问题?

好的,我成功地将其修复为:

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

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

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