Search code examples
c#wpfxamlwindows-store-apps

Set XAML Textblock content Inside a ContentTemplate


First thing My code :

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

Now ! I've got a timer, every 10sec it tries to change the text using SuggestionChange(). In fact it just says

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

and I don't find how to change that.


Solution

  • You cannot access the TextBlock defined in the DataTemplate. You can define a Dependency Property for the control and the access that property for displaying data. Refer below code.

    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>
    

    the in your code you can assign like below

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