0

I am new in WPF, so i am little lost with the "binding" and stuff like that.

I just created a controlTemplate of a custom Expander, here is my code:

<Style TargetType="{x:Type ToggleButton}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                                            <Border Padding="{TemplateBinding Padding}">
                                                <Grid SnapsToDevicePixels="False">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*"/>
                                                        <ColumnDefinition Width="6*"/>
                                                    </Grid.ColumnDefinitions>
                                                    <Border Grid.ColumnSpan="2" Name="bgHack" Background="#FFFFC000" />
                                                    <Image Grid.ColumnSpan="2" HorizontalAlignment="Center" Name="imageArrow" Source="Images/downRow.png" Stretch="None"/>
                                                    <Viewbox Grid.Column="0" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                                        <!--HERE IS THE VALUE OF THE TEXT I WANT TO CHANGE IN EACH INSTANCE-->
                                                        <TextBlock Name="headerText" Foreground="White" FontFamily="/BundyPOS;component/Fonts/#HelveticaRounded" TextAlignment="Center" Margin="7">Artículos</TextBlock>
                                                    </Viewbox>
                                                    <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
                                                </Grid>
                                            </Border>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsChecked" Value="True"><!--WHEN CLICK IS RELEASED-->
                                                    <Setter Property="Source" TargetName="imageArrow" Value="Images/upRow.png"/>
                                                </Trigger>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Background" TargetName="bgHack" Value="#3cb878"/>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>

I am also able to instance this controlTemplate like this:

<Expander Name="articlesExpander" Template="{StaticResource ExpanderHeaderImage}">
                        <StackPanel>
                                <TextBox>This is a textbox</TextBox>
                                <Button>A button</Button>
                            </StackPanel>
                     </Expander>

How can i change the text value of the TextBlock located at the controlTemplate? I mean, i am going to create several instances of this controlTemplate and i need to change the text of the TextBlock in each one.

Fernando Santiago
  • 2,128
  • 10
  • 44
  • 75
  • the `ControlTemplate` targets `ToggleButton`, the second XAML snippet does not involve that. Don't understand what you mean. If you mean you want to style the toggle button of the `Expander`, then it's not the way to go. You cannot style it from outside but inside the `Expander` (usually requires a copied template of Expander). – King King Oct 14 '15 at 02:59
  • @King King I didnt put all the code because it is too much, but the custome expander its already working, i just need to change the text of the TextBlock in the second XAML snippet – Fernando Santiago Oct 14 '15 at 03:01
  • not sure how it's much but the second snippet does not look involved the first at all. BTW you hard-coded the `Text` of the TextBlock in the ControlTemplate, so we cannot change it in XAML somewhere else. Even in code you need to perform some walking through visual tree to find the TextBlock and change the Text. If you want it to be dynamic, you need to loosely set the text (such as to some Binding, DynamicResource, ...) – King King Oct 14 '15 at 03:05
  • @FernandoSantiago Data binding TextBlock to templated parent may help you. Take a look at this [SO](http://stackoverflow.com/questions/7100251/wpf-nested-binding-in-a-controltemplate) – Siva Gopal Oct 14 '15 at 04:24

1 Answers1

0

Change the following code in your template:

<TextBlock Name="headerText" Foreground="White" 
           FontFamily="/BundyPOS;component/Fonts/#HelveticaRounded" 
           TextAlignment="Center" Margin="7">Artículos</TextBlock>

To this:

<TextBlock Name="headerText" Foreground="White" 
           FontFamily="/BundyPOS;component/Fonts/#HelveticaRounded" 
           TextAlignment="Center" Margin="7" 
           Text="{TemplateBinding Tag}" />

In your Expander's ControlTemplate, find the ToggleButton and add the following to that tag:

<ToggleButton ... Tag="{TemplateBinding Tag}" />

This is an easy way to pass a small piece of information down the chain. Now you can change or even bind the Tag property of the Expander to any text and it will be shown in your TextBlock. Here is how it would work with your second piece of code:

<Expander Name="articlesExpander" 
          Template="{StaticResource ExpanderHeaderImage}" 
          Tag="Artículos">
          ...
</Expander>
Dax Pandhi
  • 843
  • 4
  • 13