在ContentTemplate中设置XAML Textblock内容

时间:2015-03-18 14:26:57

标签: c# wpf xaml windows-store-apps

我的代码第一件事:

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()更改文本。事实上,它只是说

  

名称&#39;例句&#39;在当前上下文中不存在

并且我找不到如何改变它。

1 个答案:

答案 0 :(得分:0)

您无法访问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;
    }