开发者

TemplateBinding for Dynamic Controls

开发者 https://www.devze.com 2023-01-16 11:16 出处:网络
I create a templated RadioButton at run time. After initializing the templated RadioButton, I set the DataContext and Tag property and then add this button in a StackPanel. The problem is that the tem

I create a templated RadioButton at run time. After initializing the templated RadioButton, I set the DataContext and Tag property and then add this button in a StackPanel. The problem is that the template binding does not work. below is the XAML and the code behind. All this work if I assign values to these properties in XAML. Any ideas?

Code: TemplatedRadioButton commandButton = new TemplatedRadioButton(); commandButton.DataContext = "bla"; // Some txt that I will. commandButton.Tag = MyImage; // This is the ImageIcon that I create at run time too. MyStackPanel.Children.Add(commandButton);

XAML: 开发者_运维问答


Sorry: Code: TemplatedRadioButton commandButton = new TemplatedRadioButton(); commandButton.DataContext = "bla"; // Some txt that I will. commandButton.Tag = MyImage; // This is the ImageIcon that I create at run time too. 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>

0

精彩评论

暂无评论...
验证码 换一张
取 消