0

我创建了一个从 WPF 中的 Button 派生的自定义控件。我已经覆盖了 DefaultStyleKeyProperty,然后为 Content 设置了依赖属性。此控件对 OnMouseEnter、OnMouseLeave、OnMouseDown 和 OnMouseUp 有自己的样式。使用时,会出现事件和自定义样式,但不显示内容。如何设置内容?

该控件的代码如下:

public class SymbolButton : Button
    {
        public static readonly DependencyProperty ContentProperty;
        static SymbolButton()
        {
           DefaultStyleKeyProperty.OverrideMetadata(typeof(SymbolButton), new FrameworkPropertyMetadata(typeof(SymbolButton)));
           FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
           metadata.AffectsArrange = true;
           ContentProperty = DependencyProperty.Register("Content",
                            typeof(string), typeof(SymbolButton), new UIPropertyMetadata(null));

        }
        Point startPoint = new Point(0.5, 0);
        Point endPoint = new Point(0.5, 1);
        public string Content
        {
            get { return (string)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        public SymbolButton()
        {
            //this.Content = "Symbol";
            ButtonDefaultDisplay();
            this.BorderThickness = new Thickness(3.3);
            this.FontSize = 21;
            this.FontStyle = FontStyles.Oblique;
        }

        private void ButtonDefaultDisplay()
        {
            this.Foreground = Brushes.BlueViolet;
            this.Background = new LinearGradientBrush(Colors.LightBlue,
                                                        Colors.MediumAquamarine,
                                                        startPoint, endPoint);
            this.BorderBrush = Brushes.DarkTurquoise;
        }

        private void ButtonClickDisplay()
        {
            this.Foreground = Brushes.LightCyan;
            this.Background = new LinearGradientBrush(Colors.MidnightBlue,
                                                       Colors.DarkTurquoise,
                                                       startPoint, endPoint);
            this.BorderBrush = Brushes.Cyan;
        }

        private void ButtonHighlight()
        {
            this.Foreground = Brushes.Red;
            this.Background = new LinearGradientBrush(Colors.MistyRose,
                                                       Colors.LightBlue,
                                                       endPoint, startPoint);
            this.BorderBrush = Brushes.Plum;
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            ButtonClickDisplay();
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            ButtonHighlight();
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            ButtonHighlight();
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            ButtonDefaultDisplay();
        }
    }
4

0 回答 0