如何将Style控件的样式应用于Silverlight中的内容模板中的Child控件 [英] How to apply Style of Parent control to Child controls in content template in Silverlight

查看:45
本文介绍了如何将Style控件的样式应用于Silverlight中的内容模板中的Child控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处我的TextBox文本正在与contentemplate中的子文本框绑定,但如何将默认样式和验证错误应用于内容模板中的子文本框。

< TextBox x:Name =" tbIdNumber"宽度= QUOT; 110"高度= QUOT; 20" 
Text =" {Binding IdNumber,Mode = TwoWay,ValidatesOnExceptions = true,NotifyOnValidationError = true}">
< TextBox.Template>
< ControlTemplate TargetType =" TextBox" >
< StackPanel Orientation =" Horizo​​ntal" >
< Image Height =" 10"宽度= QUOT; 20"源= QUOT;图像/ 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>



解决方案

使用如下的TemplateBinding

 <   TextBox    x   名称  =  " tbIdNumber"    宽度  =  " 110"   高度  =  " 20"    文字  =" {  Binding    IdNumber  < span style ="color:blue">,  模式  =   TWOW ay     ValidatesOnExceptions   =   true     NotifyOnValidationError  < span style ="color:blue"> =   true  }  " &NBSP;>  
&NBSP;&NBSP;&NBSP;&NBSP ;         < TextBox。模板>
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;           < ControlTemplate   TargetType = "文本框" &NBSP;>
&NBSP;&NBSP;                    < StackPanel  方向 = " Horizo​​ntal"  >
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;  < 图片   ;高度 = " 10"   ;宽度 = " 20" <跨度风格= "颜色:红">&NBSP;来源 = "图像/ bullet_darkblue.PNG" < /跨度>的&NBSP; />
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < ; TextBox  宽度 = " 90"  标记 =" ; { TemplateBinding  标记 } "   DataContext =" { TemplateBinding   DataContext < span style ="color:blue">} "  文字 =" { TemplateBinding  文字 } " />
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; < / StackPanel >
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; < / ControlTemplate >
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < / TextBox.Template >
         < / TextBox >

Silverlight论坛是一个更好的银色问题,Silverlight专家可以帮助你


http://论坛.silverlight.net /


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>  


解决方案

Use TemplateBinding as below

<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="{TemplateBinding Tag}" DataContext="{TemplateBinding DataContext}" Text="{TemplateBinding Text}"/>
                    </StackPanel>
                </ControlTemplate>
            </TextBox.Template>
        </TextBox>

And Silverlight Forum is a better place for silverlight question where silverlight experts can help you

http://forums.silverlight.net/


这篇关于如何将Style控件的样式应用于Silverlight中的内容模板中的Child控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆