在 ContentTemplate 中设置 XAML 文本块内容

Set XAML Textblock content Inside a ContentTemplate

首先我的代码:

MainView.xaml :

<Grid Name="PropoCloud" VerticalAlignment="Bottom" Margin="0 0 0 80">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="FadeStates">
                    <VisualState x:Name="FadeOut">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="PropoCloud" Storyboard.TargetProperty="PropoCloud.Opacity" From="1" To="0" Duration="0:0:1"/>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="FadeIn">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="PropoCloud" Storyboard.TargetProperty="PropoCloud.Opacity" From="0" To="1" Duration="0:0:2"/>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <tut:TutorialAwareButton Name="PropoButton"
                                             Command="{Binding CmdCreated}"
                                             BorderThickness="0" VerticalAlignment="Bottom" 
                                             HorizontalAlignment="Left" Width="410" Height="200">
                <tut:TutorialAwareButton.ContentTemplate>
                    <DataTemplate>
                        <Grid>
                            <Image Source="../Assets/propoCloud.png" Height="168" Width="370"/>
                            <Grid Width="215" Height="69" HorizontalAlignment="Right" Margin="4">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="140"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <Grid Grid.Column="0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition/>
                                        <RowDefinition Height="4*"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Grid.Row="0" Text="Suggestion" FontFamily="Segoe UI Light" Foreground="{StaticResource BlueAppBackgroundThemeBrush}" 
                                           VerticalAlignment="Center"/>
                                    <StackPanel Grid.Row="1" VerticalAlignment="Bottom">
                                        <TextBlock x:Name="Exemples" TextWrapping="Wrap" FontSize="18" VerticalAlignment="Top" LineHeight="20"  
                                               Text=" test test test test tes tes tes testest stestestestests" MaxLines="2"
                                               FontFamily="Segoe UI Light" Foreground="{StaticResource BlueAppBackgroundThemeBrush}"/>
                                    </StackPanel>
                                </Grid>
                            </Grid>
                        </Grid>
                    </DataTemplate>
                </tut:TutorialAwareButton.ContentTemplate>
                <tut:TutorialAwareButton.CommandParameter>
                    <cmd:NavigationCommandParameter TargetName="QuestionCreatingView"></cmd:NavigationCommandParameter>
                </tut:TutorialAwareButton.CommandParameter>
            </tut:TutorialAwareButton>
        </Grid>

MainView.xaml.cs :

 int count = 0;

        private void Suggestionchange()
        {
            Exemples.Text = ExemplesQuestions[count];
            count++;
            if (count == 8) count = 0;
        }

        private void createExemple()
        {
            ExemplesQuestions = new List<string>();

            ExemplesQuestions.Add("Test1");
            ExemplesQuestions.Add("Test2");
            ExemplesQuestions.Add("Test3");
            ExemplesQuestions.Add("Test4");
            ExemplesQuestions.Add("Test5");
            ExemplesQuestions.Add("Test6");
            ExemplesQuestions.Add("Test7");
            ExemplesQuestions.Add("Test8");
        }

现在!我有一个计时器,它每 10 秒尝试使用 SuggestionChange() 更改文本。事实上它只是说

The name 'Exemples' does not exist in the current context

我不知道如何更改它。

您无法访问 DataTemplate 中定义的 TextBlock。您可以为控件定义一个依赖项 属性,并为显示数据定义一个 属性 的访问权限。参考下面的代码。

class TutorialAwareButton : Button
{
    public string RandomString
    {
        get { return (string)GetValue(RandomStringProperty); }
        set { SetValue(RandomStringProperty, value); }
    }

    public static readonly DependencyProperty RandomStringProperty =
        DependencyProperty.Register("RandomString", typeof(string), typeof(TutorialAwareButton), new PropertyMetadata(string.Empty));

}
<local:TutorialAwareButton x:Name="PropoButton"
                                         Command="{Binding CmdCreated}"
                                         BorderThickness="0" VerticalAlignment="Bottom" 
                                         HorizontalAlignment="Left" Width="410" Height="200">
        <local:TutorialAwareButton.ContentTemplate>
            <DataTemplate>                    
                <StackPanel  VerticalAlignment="Bottom">
                    <TextBlock x:Name="Exemples" TextWrapping="Wrap" FontSize="18" VerticalAlignment="Top" LineHeight="20" Text="{Binding ElementName=PropoButton,Path=RandomString}" FontFamily="Segoe UI Light" /> 
                </StackPanel>                           
            </DataTemplate>
            </local:TutorialAwareButton.ContentTemplate>
        </local:TutorialAwareButton>

在您的代码中,您可以像下面这样分配

private void Suggestionchange()
    {
        PropoButton.RandomString = ExemplesQuestions[count];
        count++;
        if (count == 8) count = 0;
    }