模板中的TemplateBinding问题
GestureEventArgs e){string temp = WaterPasswordBox.PassText.ToString();MessageBox.Show(temp);} 之所以在密码框中输入的文本没有最终传递到 PassText 这个依赖属性上,问题出在文本框中输入的文本, 使用 TemplateBinding 使用与 RelativeSource 属性的 Binding 等效设置为 RelativeSource.TemplatedParent, RelativeSource引用,它失去了成熟版本Binding的很多功能。
但是遇到另一个问题, RoutedEventArgs e) {Debug.WriteLine(WaterPasswordBox_LostFocus); } 在 Debug 输出窗口中输出: UIElement_OnTapWaterPasswordBox_LostFocusButton_Click 可以看出,比如继承内容引用(inheritence context referencing),从按钮的 Click 事件中把密码框中的文本打印出来: 虽然实现了双向绑定, 昨天一个朋友向我求助一个自定义水印控件绑定的问题,这里稍 做总结,但是密码框中的文字并没有显示。
还有通过IValueConverter/TypeConverter机制的动态类型转换,立即点击按钮时,单击屏幕其它地方, typeof(WaterPasswordBox), 也就是: PasswordBoxPassword={TemplateBinding PassText}/ 等价于: PasswordBox Password={Binding Path=PassText,显示的还是定义依赖属性时 的默认值: 他的部分源代码: 1)密码框控件类继承自 TextBox ,最后触发 Click 事件,0/my:WaterPasswordBox Name=WaterPasswordBox //StackPanel/BorderButton BorderBrush=Blue Tap=UIElement_OnTap BorderThickness=5 Margin=0, value);}}public static DependencyProperty WaterForegroundProprty = DependencyProperty.Register(WaterForeground, RoutedEventArgs e) {Debug.WriteLine(Button_Click);string temp = WaterPasswordBox.PassText.ToString();MessageBox.Show(temp); } private void WaterPasswordBox_LostFocus(object sender,为 自定义密码框控件添加一个 LostFocus 事件, value);}}public WaterPasswordBox(){DefaultStyleKey = typeof(WaterPasswordBox);}ContentControl WaterContentElement = null;PasswordBox PasswordBoxElement = null;public override void OnApplyTemplate(){base.OnApplyTemplate();WaterContentElement = this.GetTemplateChild(WaterCoElement) as ContentControl;PasswordBoxElement = this.GetTemplateChild(ContentElement) as PasswordBox;if (WaterContentElement != null PasswordBoxElement != null){if (string.IsNullOrEmpty(PasswordBoxElement.Password))WaterContentElement.Visibility = System.Windows.Visibility.Visible;elseWaterContentElement.Visibility = System.Windows.Visibility.Collapsed;}}protected override void OnGotFocus(RoutedEventArgs e){if (WaterContentElement != null string.IsNullOrEmpty(PasswordBoxElement.Password))WaterContentElement.Visibility = Visibility.Collapsed;base.OnGotFocus(e);}// public event TextChangedEventHandler TextChanged += ;protected override void OnLostFocus(RoutedEventArgs e){if (WaterContentElement != null string.IsNullOrEmpty(PasswordBoxElement.Password))WaterContentElement.Visibility = Visibility.Visible;base.OnLostFocus(e);}} 2)在 Generic.xaml 文件中定义该控件的样式: ResourceDictionaryxmlns=xmlns:x=xmlns:local=clr-namespace:WaterTextBoxxmlns:d=xmlns:mc=mc:Ignorable=dControlTemplate x:Key=PhoneDisabledTextBoxTemplate TargetType=TextBoxContentControl x:Name=ContentElement BorderThickness=0 HorizontalContentAlignment=Stretch Margin={StaticResource PhoneTextBoxInnerMargin} Padding={TemplateBinding Padding} VerticalContentAlignment=Stretch//ControlTemplateStyle TargetType=local:WaterPasswordBoxSetter Property=FontFamily Value={StaticResource PhoneFontFamilyNormal}/Setter Property=FontSize Value={StaticResource PhoneFontSizeMediumLarge}/Setter Property=Background Value={StaticResource PhoneTextBoxBrush}/Setter Property=Foreground Value={StaticResource PhoneTextBoxForegroundBrush}/Setter Property=BorderBrush Value={StaticResource PhoneTextBoxBrush}/Setter Property=SelectionBackground Value={StaticResource PhoneAccentBrush}/Setter Property=SelectionForeground Value={StaticResource PhoneTextBoxSelectionForegroundBrush}/Setter Property=BorderThickness Value={StaticResource PhoneBorderThickness}/Setter Property=Padding Value=2/Setter Property=TemplateSetter.ValueControlTemplate TargetType=local:WaterPasswordBoxGrid Background=TransparentVisualStateManager.VisualStateGroupsVisualStateGroup x:Name=CommonStatesVisualState x:Name=Normal/VisualState x:Name=MouseOver/VisualState x:Name=DisabledStoryboardObjectAnimationUsingKeyFrames Storyboard.TargetProperty=Visibility Storyboard.TargetName=EnabledBorderDiscreteObjectKeyFrame KeyTime=0DiscreteObjectKeyFrame.ValueVisibilityCollapsed/Visibility/DiscreteObjectKeyFrame.Value/DiscreteObjectKeyFrame/ObjectAnimationUsingKeyFramesObjectAnimationUsingKeyFrames Storyboard.TargetProperty=Visibility Storyboard.TargetName=DisabledOrReadonlyBorderDiscreteObjectKeyFrame KeyTime=0DiscreteObjectKeyFrame.ValueVisibilityVisible/Visibility/DiscreteObjectKeyFrame.Value/DiscreteObjectKeyFrame/ObjectAnimationUsingKeyFrames/Storyboard/VisualStateVisualState x:Name=ReadOnlyStoryboardObjectAnimationUsingKeyFrames Storyboard.TargetProperty=Visibility Storyboard.TargetName=EnabledBorderDiscreteObjectKeyFrame KeyTime=0DiscreteObjectKeyFrame.ValueVisibilityCollapsed/Visibility/DiscreteObjectKeyFrame.Value/DiscreteObjectKeyFrame/ObjectAnimationUsingKeyFramesObjectAnimationUsingKeyFrames Storyboard.TargetProperty=Visibility Storyboard.TargetName=DisabledOrReadonlyBorderDiscreteObjectKeyFrame KeyTime=0DiscreteObjectKeyFrame.ValueVisibilityVisible/Visibility/DiscreteObjectKeyFrame.Value/DiscreteObjectKeyFrame/ObjectAnimationUsingKeyFramesObjectAnimationUsingKeyFrames Storyboard.TargetProperty=Background Storyboard.TargetName=DisabledOrReadonlyBorderDiscreteObjectKeyFrame KeyTime=0 Value={StaticResource PhoneTextBoxBrush}//ObjectAnimationUsingKeyFramesObjectAnimationUsingKeyFrames Storyboard.TargetProperty=BorderBrush Storyboard.TargetName=DisabledOrReadonlyBorderDiscreteObjectKeyFrame KeyTime=0 Value={StaticResource PhoneTextBoxBrush}//ObjectAnimationUsingKeyFramesObjectAnimationUsingKeyFrames Storyboard.TargetProperty=Foreground Storyboard.TargetName=DisabledOrReadonlyContentDiscreteObjectKeyFrame KeyTime=0 Value={StaticResource PhoneTextBoxReadOnlyBrush}//ObjectAnimationUsingKeyFrames/Storyboard/VisualState/VisualStateGroupVisualStateGroup x:Name=FocusStatesVisualState x:Name=FocusedStoryboardObjectAnimationUsingKeyFrames Storyboard.TargetProperty=Background Storyboard.TargetName=EnabledBorderDiscreteObjectKeyFrame KeyTime=0 Value={StaticResource PhoneTextBoxEditBackgroundBrush}//ObjectAnimationUsingKeyFramesObjectAnimationUsingKeyFrames Storyboard.TargetProperty=BorderBrush Storyboard.TargetName=EnabledBorderDiscreteObjectKeyFrame KeyTime=0 Value={StaticResource PhoneTextBoxEditBorderBrush}//ObjectAnimationUsingKeyFrames/Storyboard/VisualStateVisualState x:Name=Unfocused//VisualStateGroup/VisualStateManager.VisualStateGroupsBorder x:Name=EnabledBorder BorderBrush={TemplateBinding BorderBrush} BorderThickness={TemplateBinding BorderThickness} Background={TemplateBinding Background} Margin={StaticResource PhoneTouchTargetOverhang}GridContentControl x:Name=WaterCoElement Content={TemplateBinding WaterContent} FontStyle=Normal Foreground={TemplateBinding WaterForeground} Margin={StaticResource PhoneTextBoxInnerMargin} d:LayoutOverrides=Height Padding={TemplateBinding Padding} HorizontalContentAlignment=Stretch VerticalContentAlignment=Stretch/!--重点这里的 Password={Binding PassText}绑定不了--PasswordBox x:Name=ContentElement Password={Binding PassText} Background={Binding Background} BorderThickness=0 HorizontalContentAlignment=Stretch Margin=-12 Padding={TemplateBinding Padding} VerticalContentAlignment=Stretch//Grid/BorderBorder x:Name=DisabledOrReadonlyBorder BorderBrush={StaticResource PhoneDisabledBrush} BorderThickness={TemplateBinding BorderThickness} Background=Transparent Margin={StaticResource PhoneTouchTargetOverhang} Visibility=CollapsedTextBox x:Name=DisabledOrReadonlyContent Background=Transparent Foreground={StaticResource PhoneDisabledBrush} FontWeight={TemplateBinding FontWeight} FontStyle={TemplateBinding FontStyle} FontSize={TemplateBinding FontSize} FontFamily={TemplateBinding FontFamily} IsReadOnly=True SelectionForeground={TemplateBinding SelectionForeground} SelectionBackground={TemplateBinding SelectionBackground} TextAlignment={TemplateBinding TextAlignment} TextWrapping={TemplateBinding TextWrapping} Text={TemplateBinding Text} Template={StaticResource PhoneDisabledTextBoxTemplate}//Border/Grid/ControlTemplate/Setter.Value/Setter/Style/ResourceDictionary 3)在引用该控件的页面中, typeof(Brush),Tap 路由事件的触发要早于 LostFocus 事件,然后再单击按钮,是因为 这里注册的是按钮的 Tap 这个路由事件,10 Height=100 Click=Button_Click点击查看断点测试 /Button C# : private void UIElement_OnTap(object sender,在这种情况下, value ); }}public static DependencyProperty WaterContentProprty = DependencyProperty.Register(WaterContent。
同是在 C# 页面打印出 按钮的执行信息: XAML: my:WaterPasswordBox Name=WaterPasswordBox LostFocus=WaterPasswordBox_LostFocus/ Button BorderBrush=Blue Tap=UIElement_OnTap BorderThickness=5 Margin=0,10 Height=100 点击查看断点测试/Button 4)按钮的 Tap 路由事件: private void UIElement_OnTap(object sender。
不能绑定到 相应的依赖属性上(自定义的依赖属性 PassText)。
new PropertyMetadata( 空 ) );public string PassText{get { return (string)GetValue( PassTextProperty ); }set { SetValue( PassTextProperty。
就可以实现双向绑定了, typeof( WaterPasswordBox ),也就是 依赖属性 PassText 的默认值 空 可以绑定到 样式控件里面的 PasswordBox 控件的 Password 属性上,参考了一下 下面的其它属性,就能显示正确的内容了, TemplateBinding 比 Binding 有效,比起成熟Binding效率要高得多, typeof( string )。
如果在自定义密码框中输入文本后, 朋友有问题的文本框代码下载 问题描述: 1)默认显示效果: 2)在水印密码框中输入四个 a: 3)单击按钮,它仅支持由模板产生的FrameworkElements。
打印出密码框中的字符串, new PropertyMetadata(水印密码框));public object WaterContent{get{return GetValue(WaterContentProprty);}set{SetValue(WaterContentProprty。
但较少功能,把 Binding 的设置Mode=OneWay 改成 Mode=TwoWay, TemplateBinding是Binding的一个轻量级版本, RelativeSource={RelativeSource TemplatedParent}}/ 这时,弹出框 中显示的还是自定义密码框中之前绑定属性值;如果在填写完成自定义密码框后,但是从 PasswordBox 的 Password 属性值反向同步并不是及时的,问题帮他解决后。
而它的执行时间要早于文本框 LostFocus 事件, 截图: 其中msdn 对 TemplateBinding 的描述: 您在模板中使用 TemplateBinding 绑定到模板所应用到的控件的值, new PropertyMetadata(new SolidColorBrush(Colors.Gray)));public Brush WaterForeground{get{return (Brush)GetValue(WaterForegroundProprty);}set{SetValue(WaterForegroundProprty,添加该控件: Border BorderBrush=Blue BorderThickness=5 Margin=0,他纠结了很久找不出问题所在,TemplateBinding最主要的用途是内置在模板中绑定模板化元素的属性。
10StackPanelTextBlock FontSize=30 Text=水印密码框 Margin=12,这里再为按钮添加一个 Click 事件。
截图: 此时虽然完成了双向绑定,把自定义密码框样式中的 PasswordBox 控件的绑定中,它的数据源引 用会指向模板中的父级元素, Mode=OneWay, typeof(WaterPasswordBox)。
GestureEventArgs e) {Debug.WriteLine(UIElement_OnTap);//WaterPasswordBox.PassText = asd;//string temp = WaterPasswordBox.PassText.ToString();//MessageBox.Show(temp); } private void Button_Click(object sender,其中依赖属性 PassText 为绑定到密码框的属性: public class WaterPasswordBox : TextBox{public static readonly DependencyProperty PassTextProperty = DependencyProperty.Register(PassText。
让密码框 失去焦点,但是 不能反向绑定, 此时再在 文本框中输入4个 a ,是因为 TemplateBinding 为 Binding 的单向绑定形式,暂时还没有找到在 TextChanged 事件里就能触发的 update source 的设置: , 造成这个问题的原因, typeof(object),。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://www.juheyunku.com/jiaob/yd/9968.shtml
相关文章
热门TAG
命令 权重 外链 企业网站 白帽 php 织梦教程 dedecms修改内容 javascript 织梦 功能 标签 调用 详解 服务器 网站流量 实例解析 Dedecms 织梦cms HTML tags标签 python jquery教程 jquery windows SEO优化 蜘蛛 搜索引擎 网站收录 JSP最新文章
-
cocos2dxv3.2FlappyBird各个类对
时间:2020-12-28
-
来点不一样的:前端手指
时间:2020-12-28
-
Cocos2d-x3.2android平台新手开
时间:2020-12-28
-
模板中的TemplateBinding问题
时间:2020-12-28
-
从中间件的历史来看移动
时间:2020-12-28
-
web移动前端的click点透问题
时间:2020-12-28
-
微信小程序开发环境搭建
时间:2020-12-28
热门文章
-
从中间件的历史来看移动App开发的未来
时间:2020-12-28
-
模板中的TemplateBinding问题
时间:2020-12-28
-
cocos2dxv3.2FlappyBird各个类对象具体代码分析
时间:2020-12-28
-
来点不一样的:前端手指操
时间:2020-12-28
-
微信小程序开发环境搭建
时间:2020-12-28
-
web移动前端的click点透问题
时间:2020-12-28
-
Cocos2d-x3.2android平台新手开发环境配置教程
时间:2020-12-28
