我有TabControl,我有计划生成dynamicaly TabItems并填写ControlTemplate。
的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”和RorateTransform Angle为“progress_bar”,但我无法访问模板中的任何控件。
我试过了:
TextBlock tb = (TextBlock)newControl.Template.FindName("Qbody", newControl);
但它会返回null
。
任何人都可以帮助我。 我不知道我做错了什么。 是否有任何其他方式来设置这两个控件。
答案 0 :(得分:1)
你将此标记为mvvm,但你所做的事情违反了mvvm原则。您的进度条和文本框应绑定到您的viewmodel,然后您可以访问它们的属性。
您的控件为空,因为它们尚未创建。
但是如果你想使用后面的代码,你需要访问框架元素的应用模板事件中的控件(Tab Control)
干杯
编辑1:
您必须实现iNotifyPropertyChanged才能让UI“了解”viewmodel中属性的更改。您可以为属性计数器的每次迭代引发此事件。
此外,您需要的范围不能涵盖在一个答案中。我建议阅读更多关于MVVM和WPF的内容,也许可以使用现有的框架来帮助像MVVM-Light或Caliburn.Micro。