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>

 

相关文章:

  • 2021-11-13
  • 2022-12-23
  • 2021-09-06
  • 2021-11-10
  • 2021-09-10
  • 2020-10-15
  • 2022-12-23
  • 2021-10-29
猜你喜欢
  • 2021-09-04
  • 2021-10-11
  • 2022-02-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案