簡體   English   中英

WPF在ControlTemplate中的TextBlock上更改文本屬性

[英]WPF change text property on TextBlock in ControlTemplate

我有TabControl,並且我有一個計划來動態生成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”和橢圓“ progress_bar”。 我正在嘗試將文本添加到“ Qbody”和“ progress_bar”的RorateTransform Angle中,但是我無法訪問模板中的任何控件。

我試過了:

TextBlock tb = (TextBlock)newControl.Template.FindName("Qbody", newControl);

但是它返回null

誰能幫助我。 我不知道我在做什么錯。 設置這兩個控件的任何其他方法。

您已將其標記為mvvm,但是您所做的違反了mvvm原則。 您的進度條和文本框應該綁定到您的視圖模型,然后您可以在那里訪問其屬性。

您的控件為空,因為尚未創建。

但是,如果要在后面使用代碼,則需要在framework元素的apply template事件中訪問控件(制表符控件)

框架ApplyTemplate事件

干杯

編輯1:

您必須實現iNotifyPropertyChanged才能使UI“了解”視圖模型中的屬性更改。 您將在屬性計數器的每次迭代中引發此事件。

同樣,您所需要的范圍不能在一個答案中涵蓋。 我建議閱讀更多有關MVVM和WPF的信息,並可能使用現有的框架來提供幫助,例如MVVM-Light或Caliburn.Micro。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM