我在 WPF/XAML 中有一个 TextBox 的子类,我希望对它应用与所有其他 TextBox 实例相同的样式。我定义了以下样式
<Style TargetType="TextBox" x:Key="basicTextBox" >
<Setter Property="Controls:TextBoxBehaviours.UpdateWhenEnterPressed" Value="True"/>
<Setter Property="Controls:TextBoxBehaviours.SelectAllWhenEnterPressed" Value="True"/>
<Setter Property="Controls:TextBoxBehaviours.SelectAllOnFocus" Value="True"/>
</Style>
和一个类 TextBoxBehaviours 来实现这些
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Reactive.Linq;
namespace Weingartner.Controls
{
public static class TextBoxBehaviours
{
static TextBoxBehaviours()
{}
#region Binding Support
private static Dictionary<Tuple<TextBox,string>, IDisposable> Bindings
= new Dictionary<Tuple<TextBox,string>, IDisposable>();
private static void Bind(TextBox tb, string key, IDisposable d)
{
Bindings[Tuple.Create(tb, key)] = d;
}
private static void UnBind(TextBox tb, string key)
{
var t = Tuple.Create(tb, key);
if (Bindings.ContainsKey(t))
{
var d = Bindings[t];
Bindings.Remove(t);
d.Dispose();
}
}
#endregion
static UIPropertyMetadata CreateMeta<T>(bool defaultValue, Action<T,DependencyPropertyChangedEventArgs> fn)
where T : DependencyObject
{
return new UIPropertyMetadata(defaultValue, (o, e) =>{
var t = o as T;
if (t == null)
{
return;
}
fn(t, e);
});
}
#region Update When Enter Pressed
public static readonly DependencyProperty
UpdateWhenEnterPressedProperty =
DependencyProperty.RegisterAttached
( "UpdateWhenEnterPressed"
, typeof(bool)
, typeof(TextBoxBehaviours)
, CreateMeta<TextBox>(false, SetupUpdateOnEnterPressed));
public static void
SetUpdateWhenEnterPressed
( TextBox dp
, bool value)
{
dp.SetValue(UpdateWhenEnterPressedProperty, value);
}
public static bool
GetUpdateWhenEnterPressed
( TextBox dp)
{
return (bool)dp.GetValue(UpdateWhenEnterPressedProperty);
}
private static void
SetupUpdateOnEnterPressed
( TextBox element
, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
var sub = element
.PreviewKeyDownObserver()
.Where(x=>x.EventArgs.Key==Key.Enter)
.Subscribe(x=>DoUpdateSource(element));
Bind(element, "KeyEnter", sub);
}else{
UnBind(element, "KeyEnter");
}
}
static void DoUpdateSource(TextBox source)
{
BindingExpression binding = BindingOperations.GetBindingExpression(source, TextBox.TextProperty);
if (binding != null)
{
binding.UpdateSource();
}
}
#endregion
#region SelectAll When Enter Pressed
public static readonly DependencyProperty
SelectAllWhenEnterPressedProperty =
DependencyProperty.RegisterAttached
( "SelectAllWhenEnterPressed"
, typeof(bool)
, typeof(TextBoxBehaviours)
, CreateMeta<TextBox>(false, SetupSelectAllOnEnterPressed));
public static void
SetSelectAllWhenEnterPressed
( TextBox dp
, bool value)
{
dp.SetValue(SelectAllWhenEnterPressedProperty, value);
}
public static bool
GetSelectAllWhenEnterPressed
( TextBox dp)
{
return (bool)dp.GetValue(SelectAllWhenEnterPressedProperty);
}
private static void
SetupSelectAllOnEnterPressed
( TextBox element
, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
var sub = element
.PreviewKeyDownObserver()
.Where(x=>x.EventArgs.Key==Key.Enter)
.Subscribe(x=>element.SelectAll());
Bind(element, "KeyEnterSelectAll", sub);
}else{
UnBind(element, "KeyEnterSelectAll");
}
}
#endregion
#region Select All On Focus
public static readonly DependencyProperty
SelectAllOnFocusProperty =
DependencyProperty.RegisterAttached
( "SelectAllOnFocus"
, typeof(bool)
, typeof(TextBoxBehaviours)
, CreateMeta<TextBox>(false, SetupSelectAllOnFocus));
public static void
SetSelectAllOnFocus
( TextBox dp
, bool value)
{
dp.SetValue(SelectAllOnFocusProperty, value);
}
public static bool
GetSelectAllOnFocus
( TextBox dp)
{
return (bool)dp.GetValue(SelectAllOnFocusProperty);
}
private static void
SetupSelectAllOnFocus
( TextBox element
, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
Bind(element, "Focus", element.GotFocusObserver().Subscribe(x => element.SelectAll()));
}else{
UnBind(element, "Focus");
}
}
#endregion
}
}
在我一直在做的 XAML 文件中
<Style TargetType="TextBox" BasedOn="{StaticResource basicTextBox}"/>
并且所有以下文本框都得到我希望的行为。鉴于我的子类也是一个 TextBox 类,我认为这些也会得到这种行为,但它们不会。我尝试通过扩展样式来明确
<Style TargetType="TextBox" BasedOn="{StaticResource basicTextBox}"/>
<Style TargetType="Controls:EditForLength">
<Setter Property="Controls:TextBoxBehaviours.UpdateWhenEnterPressed" Value="True"/>
<Setter Property="Controls:TextBoxBehaviours.SelectAllWhenEnterPressed" Value="True"/>
<Setter Property="Controls:TextBoxBehaviours.SelectAllOnFocus" Value="True"/>
</Style>
但我得到一个错误,告诉我关键EditForLength
System.ArgumentException occurred
HResult=-2147024809
Message=Item has already been added. Key in dictionary: 'Weingartner.Controls.EditForLength' Key being added: 'Weingartner.Controls.EditForLength'
Source=mscorlib
StackTrace:
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
InnerException:
因此,总而言之,通过父类上的样式安装的行为不会传播到子类实例,但是当尝试添加另一种针对子类的样式时,会出现一些字典错误。
注意我已经在EditForLength
控件的实例上尝试了明确的行为,它们可以正常工作。我只是无法通过样式安装行为。
更新
发现即使除样式声明之外的所有重要位都被注释掉时,我也会收到错误
<!--<Style TargetType="TextBox" BasedOn="{StaticResource basicTextBox}"/>-->
<Style TargetType="Controls:EditForLength">
<!--
<Setter Property="Controls:TextBoxBehaviours.UpdateWhenEnterPressed" Value="True"/>
<Setter Property="Controls:TextBoxBehaviours.SelectAllWhenEnterPressed" Value="True"/>
<Setter Property="Controls:TextBoxBehaviours.SelectAllOnFocus" Value="True"/>
-->
</Style>