WPF控件的扩展附件属性
1.附加属性,采用依赖属性注册附件的方式:DependencyProperty.RegisterAttached,get与set要单独写,都是静态的;
#region HintFontWeight 字体加粗附加属性 public static readonly DependencyProperty HintFontWeightProperty = DependencyProperty.RegisterAttached ( "HintFontWeight", typeof(FontWeight), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(FontWeights.Normal) ); public static FontWeight GetHintFontWeight(DependencyObject obj) { return (FontWeight)obj.GetValue(HintFontWeightProperty); } public static void SetHintFontWeight(DependencyObject obj, FontWeight value) { obj.SetValue(HintFontWeightProperty, value); } #endregion
附件属性绑定到原生控件上,需要改控件的样式或模板
<Grid Grid.Column="1"> <TextBlock x:Name="Message" Padding="0" Visibility="Visible" Text="{TemplateBinding local:ControlAttachProperty.Hint}" FontWeight="{TemplateBinding local:ControlAttachProperty.HintFontWeight}" FontStyle="{TemplateBinding local:ControlAttachProperty.HintFontStyle}" Foreground="{TemplateBinding Foreground}" IsHitTestVisible="True" Opacity="0.5" HorizontalAlignment="Left" TextAlignment="Center" VerticalAlignment="Center" Margin="5,2,5,2" /> </Grid>
![](https://files-cdn.cnblogs.com/files/jiangyan219/Alipay.bmp)
您的资助是我最大的动力!
金额随意,欢迎来赏!
![](https://files-cdn.cnblogs.com/files/jiangyan219/WeChat.bmp)
我写的东西能让你能懂,那是义务
毕竟占用了你生命中的宝贵的时间和注意力
要是你还能喜欢我的作品,那就是缘分了
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【青青子衿】!