2010-09-17 82 views
0

我在运行时创建了一个模板化的RadioButton。初始化模板化的RadioButton之后,我设置了DataContext和Tag属性,然后在StackPanel中添加此按钮。问题是模板绑定不起作用。下面是XAML和后面的代码。如果我在XAML中为这些属性赋值,所有这些工作。有任何想法吗?动态控件的模板绑定

代码: TemplatedRadioButton commandButton = new TemplatedRadioButton(); commandButton.DataContext =“bla”; //我会的一些txt。 commandButton.Tag = MyImage; //这也是我在运行时创建的ImageIcon。 MyStackPanel.Children.Add(commandButton);

XAML:

回答

0

对不起: 代码: TemplatedRadioButton的commandButton =新TemplatedRadioButton(); commandButton.DataContext =“bla”; //我会的一些txt。 commandButton.Tag = MyImage; //这也是我在运行时创建的ImageIcon。 MyStackPanel.Children.Add(commandButton);

XAML:

<Grid Margin="0 8 0 1"> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="4*"/> 
    <RowDefinition Name="textheight" Height="2*"/> 
    </Grid.RowDefinitions> 
<ContentPresenter x:Name="Content" ContentSource="Tag" Margin=" 4 4 6 6" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 

    <TextBlock Name="caption" Text="{TemplateBinding DataContext}" FontSize="11" FontFamily="/Fonts/#Lucida Grande" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="#FF313131" Margin="0 2 0 6"/> 
</Grid> 
</Border>