I created a custom control which is a TextBox that contains a TexBlock inside. This TexBlock dissapear if the place holder text(Directorio Archivo) is empty. It works and it was perfect for my initianl needs, but now I need to reuse that custom control with other place holder text. I am new on WPF and its a little overwhelming for me right now. The following code shows a working template for one specific situation:
<Style x:Key="DefaultTextBox" TargetType="TextBox">
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="Margin" Value="10, 5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border Background="#ffffff" BorderBrush="#000000" BorderThickness="1" CornerRadius="5" >
<Grid>
<TextBox x:Name="Directorio" Background="Transparent" BorderThickness="0" Margin="5" VerticalContentAlignment="Center" Text="{TemplateBinding Text}"/>
<TextBlock Foreground="#828282" HorizontalAlignment="Center" IsHitTestVisible="False" Margin="5" Text="Directorio Archivo">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=Directorio}" Value="">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="Visibility" Value="Hidden"/>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now I want to re use the template and replace the text "Directorio Archivo" for something defined on the control. This portion of code I want to change:
<TextBlock Foreground="#828282" HorizontalAlignment="Center" IsHitTestVisible="False" Margin="5" Text="{Templatebinding or binding?????}">
Now if I could just replace the text of that place holder field calling it like this for example:
<TextBox Grid.Row="0" Grid.Column="1" Margin="5,5,20,5" Style="{StaticResource DefaultTextBox}" TextBlock.Text="MyPlaceHolderText"/>
I have tried everything but I am struggling to understand how WPF works and its documentation is not very easy to follow.
Thanks In advance
CodePudding user response:
You either need to define a custom dependency property to store the value of the text, or you could use an existing property such as Tag
:
<TextBox ... Tag="Directorio Archivo" />
You then modify the template to bind to this property:
<TextBlock Foreground="#828282" ... Text="{TemplateBinding Tag}">