我在运行时创建一个模板化的RadioButton。在初始化模板化的RadioButton之后,我设置了DataContext和Tag属性,然后将此按钮添加到StackPanel中。问题是模板绑定不起作用。下面是XAML和后面的代码。如果我在XAML中为这些属性赋值,所有这些都会起作用。有什么想法吗?
代码: TemplatedRadioButton commandButton = new TemplatedRadioButton();commandButton.DataContext = "bla";//我将使用一些txt。commandButton.Tag = MyImage;//这也是我在运行时创建的ImageIcon。MyStackPanel.Children.Add(commandButton);
XAML:
发布于 2010-09-18 05:43:31
抱歉:代码: TemplatedRadioButton commandButton = new 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>https://stackoverflow.com/questions/3739234
复制相似问题