我有 TabControl,我有一个计划动态生成 TabItems 并用 ControlTemplate 填充。
控制模板:
<Window.Resources>
<ControlTemplate x:Key="Qtemp" TargetType="Control">
<ControlTemplate.Resources>
<ControlTemplate x:Key="yesButton" TargetType="{x:Type Button}">
<Grid>
<Image Visibility="Visible" Name="Normal" Source="/TabItemTemplate;component/Images/yes.png" />
<Image Visibility="Hidden" Name="Pressed" Source="/TabItemTemplate;component/Images/yes_p.png" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsPressed" Value="True">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="noButton" TargetType="{x:Type Button}">
<Grid>
<Image Visibility="Visible" Name="Normal" Source="/TabItemTemplate;component/Images/no.png" />
<Image Visibility="Hidden" Name="Pressed" Source="/TabItemTemplate;component/Images/no_p.png" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="circleButton">
<Grid>
<Ellipse>
<Ellipse.Fill>
<ImageBrush ImageSource="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" />
</Ellipse.Fill>
</Ellipse>
</Grid>
</ControlTemplate>
</ControlTemplate.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Ellipse Grid.Row="1" Margin="-1024,-75,0,0" Name="progress_bar" Fill="#FF01325E" Width="424" Height="424" StrokeThickness="0" RenderTransformOrigin=".5,.5">
<Ellipse.Clip>
<RectangleGeometry Rect="0,0,212,424"/>
</Ellipse.Clip>
<Ellipse.RenderTransform>
<RotateTransform Angle="45"/>
</Ellipse.RenderTransform>
</Ellipse>
<Grid Grid.RowSpan="3" Grid.Row="0" Width="Auto" Height="Auto" >
<Grid.Background>
<ImageBrush ImageSource="/TabItemTemplate;component/Images/Q_bg.png" />
</Grid.Background>
</Grid>
<TextBlock Grid.Row="1" Name="QBody" Width="400" Margin="-120,-100,0,0" Foreground="#FF333333" TextWrapping="Wrap" FontFamily="Arial" FontSize="28" TextAlignment="Left" FontWeight="Bold" VerticalAlignment="Center" Text="{Binding}" />
<StackPanel Grid.Row="1" Width="350" HorizontalAlignment="Right">
<Button Width="172" Height="172" Name="BT_yes" Template="{StaticResource yesButton}" Click="BT_NextQ" />
<Button Width="172" Height="172" Name="BT_no" Margin="0,20,0,0" Template="{StaticResource noButton}" Click="BT_NextQ" />
</StackPanel>
<Image Grid.Row="2" Source="/TabItemTemplate;component/Images/toolbar.png" />
<Button Grid.Row="2" Width="56" Height="56" Tag="/TabItemTemplate;component/Images/home.png" HorizontalAlignment="Left" Margin="90,0,0,15" Template="{StaticResource circleButton}" />
<Button Grid.Row="2" Width="56" Height="56" Tag="/TabItemTemplate;component/Images/back.png" HorizontalAlignment="Left" Margin="26,0,0,15" Template="{StaticResource circleButton}" />
</Grid>
</ControlTemplate>
</Window.Resources>
在 XAML 中:
<TabControl HorizontalAlignment="Stretch" Name="navTab" VerticalAlignment="Stretch" BorderThickness="0" Padding="0">
<TabItem Name="tabItem1a" Header="static">
<Control Template="{StaticResource Qtemp}" />
</TabItem>
</TabControl>
CS文件:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 6; i++)
{
TabItem newTab = new TabItem();
Control newControl = new Control();
newControl.Template = (ControlTemplate)FindResource("Qtemp");
//TextBlock tb = (TextBlock)newControl.Template.FindName("Qbody", newControl);
//tb.Text = "TEST" + i.ToString();
newTab.Header = "Dynamic"+i.ToString();
newTab.Name = "Question" + i.ToString();
newTab.Content = newControl;
navTab.Items.Add(newTab);
}
}
在模板中,我有 TextBlock“Qbody”和 Ellipse“progress_bar”。我正在尝试将文本添加到“Qbody”和“progress_bar”的 RorateTransform Angle,但我无法访问模板中的任何控件。
我试过了:
TextBlock tb = (TextBlock)newControl.Template.FindName("Qbody", newControl);
但它返回null
。
任何人都可以帮助我。我不知道我做错了什么。是否有任何其他方式来设置这两个控件。