文章目录
Attached Properties
这一节主要实现Attached Properties附加属性的封装,并以PasswordBox为例展示应用
1.Texts.xaml
在上一节Creating Login Form Sign Up Screen中,我们实现了TextBox的基本样式,而PasswordBox本质上是与TextBox类似的,但由于数据加密的原因,PasswordBox并没有直接提供MVVM的bingding,视频中开头有说到这个,这是为什么直接复制TextBox的样式而不能实现空值提示的原因。
在分析PasswordBox的实现之前,先分析一下上一节的TextBox怎么实现的空值提示
关键的地方是RelativeSource={RelativeSource TemplatedParent}中的TemplatedParent,对于RelativeSource 其实我们平时还会经常用到另一个值Self,我们去查看Microsoft的解释RelativeSourceMode Enum,这是一个枚举,描述的是当前控件位置与binding对象的位置层级(距离),这里的枚举数字值0-3并不如数字所示,关键是看解释和字面意思,Self是指当前的控件,TemplatedParent则指的是当前修饰控件的数据binding对象(TextBox),这里有一篇关于TemplatedParent的描述
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Collapsed"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Text,RelativeSource={RelativeSource TemplatedParent}}" Value="">
<Setter Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
如果我们使用Self会是什么效果,这里我修改下代码,那么这个“Name”的空值提示会一直存在,而如果Value不等于Name的话,那么这个空值提示则一直不会出现,因为这个Name是在TextBox的Tag里面是一个固定值,层层传递到了这里的TextBlock也是固定值,DataTrigger 也只会是固定的
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Collapsed"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Text,RelativeSource={RelativeSource Self}}" Value="Name">
<Setter Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
2.PasswordBox
对于PasswordBox,Password属性不像TextBox那样的Text属性那样是依赖属性(主要因为security加密的原因),对于要使PasswordBox支持属性binding,网上也有不少的实现,有使用依赖属性的,也有使用附加属性的,本篇使用的是后者。之于依赖属性和附加属性的区别,要详细解释,得花好几个篇幅都不一定能说清楚,这里引用大牛的文章,有兴趣研究的可以细看WPF之依赖属性和附加属性,
这里我简单的概括一下两者的区别,依赖属性是发生在自己本身,而附加属性是使用时附加上自己(类似AOP)拿TextBox来说,输入空格是,可以智能提示出来的都是自己本身的属性(依赖属性,clr属性等),而对于自己编写的附加属性,则是不会智能提示出来,使用时需要自行书写形式规则。
3.PasswordBoxProperties.cs
这里有两个附加属性的实现,MonitorPasswordProperty和HasTextProperty属性,前者用来监听PasswordBox密码的变化,后者则是提供一个判断是否密码值为空,继而实现PasswordBox的空值提示。
public class MonitorPasswordProperty : BaseAttachedProperties<MonitorPasswordProperty, bool>
{
public override void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var passwordBox = sender as PasswordBox;
if (passwordBox == null) return;
passwordBox.PasswordChanged -= PasswordBox_PasswordChanged;
if ((bool)e.NewValue)
{
HasTextProperty.SetValue(passwordBox, passwordBox.SecurePassword.Length > 0);
passwordBox.PasswordChanged += PasswordBox_PasswordChanged;
}
}
private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
HasTextProperty.SetValue((PasswordBox)sender);
}
}
/// <summary>
/// the hastext attached property for a passwordbox
/// </summary>
public class HasTextProperty : BaseAttachedProperties<HasTextProperty, bool>
{
public static void SetValue(DependencyObject sender)
{
SetValue(sender, ((PasswordBox)sender).SecurePassword.Length > 0);
}
}
4.BaseAttachedProperties.cs
对于这个文件,则是对依赖属性类的封装,这里的写法都是很标准的,细腻之处在于使用了泛型参数实现动态封装性,并且对Parent的限制也是继承者必定是实现依赖属性的保证。
/// <summary>
/// a base attached property to replace the vanilla wpf attached property
/// </summary>
/// <typeparam name="Parent">the parent class to be the attached property</typeparam>
/// <typeparam name="Property">the parent class to be the attached property</typeparam>
public abstract class BaseAttachedProperties<Parent,Property>
where Parent : BaseAttachedProperties<Parent, Property>, new()
{
#region public events
/// <summary>
/// fired when the value changed
/// </summary>
public event Action<DependencyObject, DependencyPropertyChangedEventArgs> ValueChanged = (sender, e) => { };
#endregion
#region public Properties
/// <summary>
/// a singleton instance of our parent class
/// </summary>
public static Parent Instance { get; private set; } = new Parent();
#endregion
#region attached property definitions
public static readonly DependencyProperty ValueProperty =
DependencyProperty.RegisterAttached("Value", typeof(Property), typeof(BaseAttachedProperties<Parent, Property>), new PropertyMetadata(new PropertyChangedCallback(OnValuePropertyChanged)));
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Instance.OnValueChanged(d,e);
Instance.ValueChanged(d, e);
}
public static Property GetValue(DependencyObject d) => (Property)d.GetValue(ValueProperty);
//{
// return (Property)d.GetValue(ValueProperty);
//}
public static void SetValue(DependencyObject d, Property value) => d.SetValue(ValueProperty, value);
#endregion
#region event methods
public virtual void OnValueChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e) { }
#endregion
}
5.Texts.xaml
我们再次回到Texts的样式定义处,可以看到这里使用上了刚刚定义的MonitorPasswordProperty和HasTextProperty,可以注意到他们的使用都是带上了local,而这个local则是在我们的Faseto.Word命名空间下,而不是在这个控件下,这就是附加属性,这里的逻辑就是binding ,HasTextProperty 的bool值,然后做一个converter转换来实现显示和关闭
<Setter Property="local:MonitorPasswordProperty.Value" Value="True"></Setter>
......
......
<TextBlock IsHitTestVisible="False"
Text="{TemplateBinding Tag}"
x:Name="placeholder"
FontFamily="{StaticResource LatoThin}"
Padding="{TemplateBinding Padding}"
Visibility="{TemplateBinding local:HasTextProperty.Value,Converter={local:BooleanToVisiblityConverter}}"
VerticalAlignment="Center"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Foreground="{StaticResource ForegroundDardBrush}"
>
</TextBlock>
6.BooleanToVisiblityConverter.cs
这个则是一个用bool值来控制控件的显隐,这个也是经常使用到
public class BooleanToVisiblityConverter : BaseValueConverter<BooleanToVisiblityConverter>
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Hidden : Visibility.Visible;
}
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
7.视频链接
链接:https://pan.baidu.com/s/1okH2LkQT0hKDWQPU9CGxNw
提取码:us84
last modified:2018年10月18日15:56:32(1)