在ContentTemplate内设置XAML Textblock内容

阿德里安·布莱索(Adrien Blaizot)

第一件事我的代码:

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()实际上它只是说

名称“示例”在当前上下文中不存在

而且我没有找到如何改变它的方法。

阿亚潘(Ayyappan)Subramanian

您无法访问在DataTemplate中定义的TextBlock。您可以为控件定义一个Dependency属性,并可以访问该属性以显示数据。请参考下面的代码。

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;
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章