c#,使用WPF的Adorner实现iPhone上新邮件或消息提示效果----实现(二)

一、背景介绍

    在上一篇《c#,使用WPF的Adorner实现iPhone上新邮件或消息提示效果----实现(一)》中,我们通过PromptableButton,PromptAdorner,PromptChrome实现提示效果,其中PromptableButton提供PromptCount代表提示数量供PromptChrome绑定显示,PromptChrome负责显示提示效果,而PromptAdorner负责呈现PromptChrome。

    但是,这里有个问题,为了要使用PromptAdorner这个装饰件,难道所有的控件都要像PromptableButton控件一样提供PromptCount供装饰件绑定? 如果这样做,如果样装饰一个图片控件,那我们又要继承重写一个?如果直接要在常用控件上直接使用装饰件不就行不通了? 如果不能解决这3个问题,那么耦合度还是太高,通用性也不强。

    基于上面的问题,我们采用依赖属性中另外一个利器--“附加属性”,比如:我们要在Canvas内放置一个按钮并且指定其位置时我们都会写<Button Canvas.Left="100" Canvas.Top="100"/>,这里的Canvas.Left就是附加属性的实际使用。以此参考方法,我们也通过编写附件属性来改进目前的问题。

 

二、改进分解说明

    1、改进PromptButton。

    该自定义按钮内不再拥有PromptCount属性,因为我们要把依赖属性挪挪地方了。

internal class PromptableButton : Button {

        public ImageSource CoverImageSource {
            get { return (ImageSource)GetValue(CoverImageSourceProperty); }
            set { SetValue(CoverImageSourceProperty, value); }
        }

        public static readonly DependencyProperty CoverImageSourceProperty =
            DependencyProperty.Register("CoverImageSource", typeof(ImageSource), typeof(PromptableButton), new UIPropertyMetadata(null));


        public PromptableButton() {
            
        }
        
        static PromptableButton() {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PromptableButton), new FrameworkPropertyMetadata(typeof(PromptableButton)));
        }
    }


    2、改进Prom

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WPF中,我们可以使用控件的属性来实现文本框和密码框的水印效果(也称为背景文字提示)。下面是实现效果的一些方法: 方法一:使用控件的Placeholder属性 WPF的文本框和密码框控件都有一个名为Placeholder的属性,该属性可以用来显示文本框或密码框中的默认提示文本。当用户在文本框或密码框中输入内容后,该提示文本就会自动隐藏。 例如,以下代码演示了在WPF中如何使用Placeholder属性来实现文本框的水印效果: ```xml <TextBox Placeholder="请输入文本"></TextBox> ``` 同样地,我们可以在密码框中使用Placeholder属性来实现密码框的水印效果: ```xml <PasswordBox Placeholder="请输入密码"></PasswordBox> ``` 方法使用控件的Adorner WPF中的Adorner是一种用于在控件上添加装饰元素的机制。我们可以使用Adorner实现文本框和密码框的水印效果。 以下是实现效果的一般步骤: 1.定义一个自定义控件,例如WatermarkTextBox。 2.在WatermarkTextBox中定义一个名为Watermark的依赖属性。该属性表示水印提示文本。 3.重写WatermarkTextBox的OnRender方法,使其在控件上绘制水印提示文本。 4.在WatermarkTextBox的TextChanged事件中判断文本框中是否有内容,如果有则隐藏水印提示文本,否则显示水印提示文本。 以下是WatermarkTextBox的示例代码: ```csharp public class WatermarkTextBox : TextBox { public static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register( "Watermark", typeof(string), typeof(WatermarkTextBox), new PropertyMetadata(default(string))); public string Watermark { get { return (string)GetValue(WatermarkProperty); } set { SetValue(WatermarkProperty, value); } } protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); if (string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(Watermark)) { var formattedText = new FormattedText( Watermark, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Foreground); drawingContext.DrawText(formattedText, new Point(2, 2)); } } protected override void OnTextChanged(TextChangedEventArgs e) { base.OnTextChanged(e); if (string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(Watermark)) { InvalidateVisual(); } } } ``` 使用WatermarkTextBox时,只需要在XAML中设置Watermark属性即可: ```xml <local:WatermarkTextBox Watermark="请输入文本"></local:WatermarkTextBox> ``` 同样地,我们也可以定义一个WatermarkPasswordBox类来实现密码框的水印效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值