U
U
uzi_no_uzi2021-04-14 00:08:21
WPF
uzi_no_uzi, 2021-04-14 00:08:21

How to set the TemplateBinding correctly?

There is such a Template

<ControlTemplate x:Key="TextBoxFieldTemplate" TargetType="{x:Type TextBox}">
            <Grid Height="{TemplateBinding Height}">
                <TextBox 
                                Foreground="{TemplateBinding Foreground}"
                                FontFamily="{TemplateBinding FontFamily}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                Padding="{TemplateBinding Padding}"
                                FontSize="16"
                                Height="30"
                                Text="{TemplateBinding Text}"/>
                <Image Source="C:\Users\user\Downloads\usr.png" Width="14" Height="14" HorizontalAlignment="Left" />
            </Grid>
        </ControlTemplate>


Inside is <Image />how do I pass the value to the Source, how to correctly write the TemplateBinding to such elements.

This is how the element itself is created, the specified values ​​​​are transferred to the Template and, accordingly, to the TextBox, how to transfer the value in <image />the same way?

<TextBox 
                Tag="C:\Users\user\Downloads\usr.svg"
                Padding="20 4 0 0" 
                Foreground="#ccc" 
                Text="username"
                BorderThickness="0 0 0 1"  
                BorderBrush="#ccc"
                Template="{StaticResource TextBoxFieldTemplate}"
                FontFamily="/Fonts/#Montserrat Light" Margin="0,50,0,100" TextChanged="TextBox_TextChanged_1">
            </TextBox>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2021-04-14
@yarosroman

1. Use AttachedDependencyProperty (see the last answer https://stackoverflow.com/questions/7309623/pass-p... )
2. Implement your improved TextBox as a user control and the corresponding class and add the DependencyProperty you need already. One of the advantages here is that, if necessary, you can, if necessary, override some control methods to change the behavior, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question