简体   繁体   中英

How to apply Style of Parent control to Child controls in content template in Silverlight

Here My TextBox text is being bind to child text box in contentemplate but how to apply along with default style and validation errors to the child textbox in content template.

    <TextBox x:Name="tbIdNumber"  Width="110" Height="20"  Text="{Binding IdNumber, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" >
        <TextBox.Template>
            <ControlTemplate TargetType="TextBox" >                    
                <StackPanel Orientation="Horizontal" >
                   <Image Height="10" Width="20" Source="Images/bullet_darkblue.PNG" />
                    <TextBox   Width="90"   Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}"    DataContext="{Binding RelativeSource={RelativeSource TemplatedParent},Path=DataContext, Mode=TwoWay}"
                               Text="{Binding  Path=Text ,RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}"  >                          
                    </TextBox>
                </StackPanel>
             </ControlTemplate>
        </TextBox.Template>
    </TextBox>

It is very unusual to define the control template for TextBox control with a TextBox. I suggest you not pursue the path above.

You should instead start off with the default ControlTemplate for a TextBox here (Warning: it is very complex). It contains all the necessary visual states for validation, focused, etc.

From what I observe, you want to add an image next to the textbox. You should be able to add an image element inside the Grid and then perhaps adjust the margin on the Border. Here's a snippet:

<Image Height="10" Width="20" Source="Images/bullet_darkblue.PNG" />
<Border Margin="20,10,0,0" 
        x:Name="MouseOverBorder" BorderThickness="1" BorderBrush="Transparent">
  <ScrollViewer x:Name="ContentElement" Padding="{TemplateBinding Padding}" 
     BorderThickness="0" IsTabStop="False"/>
</Border>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM