自动锁定类
我编写了一个“AutomaticLock”类,它具有继承的附加“DoLock”属性。
将“DoLock”属性设置为 true 会将所有文本框组合框、复选框等重新模板化为文本块、不可编辑的复选框等。我的代码设置为使其他附加属性可以指定要在锁定(“视图”)模式下使用的任意模板、不应该自动锁定的控件等。
因此,可以轻松地使用同一个视图进行编辑和查看。设置单个属性来回更改它,并且它是完全可自定义的,因为视图中的任何控件都可以触发“DoLock”属性以任意方式更改其外观或行为。
实现代码
代码如下:
public class AutomaticLock : DependencyObject
{
Control _target;
ControlTemplate _originalTemplate;
// AutomaticLock.Enabled: Set true on individual controls to enable locking functionality on that control
public static bool GetEnabled(DependencyObject obj) { return (bool)obj.GetValue(EnabledProperty); }
public static void SetEnabled(DependencyObject obj, bool value) { obj.SetValue(EnabledProperty, value); }
public static readonly DependencyProperty EnabledProperty = DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(AutomaticLock), new FrameworkPropertyMetadata
{
PropertyChangedCallback = OnLockingStateChanged,
});
// AutomaticLock.LockTemplate: Set to a custom ControlTemplate to be used when control is locked
public static ControlTemplate GetLockTemplate(DependencyObject obj) { return (ControlTemplate)obj.GetValue(LockTemplateProperty); }
public static void SetLockTemplate(DependencyObject obj, ControlTemplate value) { obj.SetValue(LockTemplateProperty, value); }
public static readonly DependencyProperty LockTemplateProperty = DependencyProperty.RegisterAttached("LockTemplate", typeof(ControlTemplate), typeof(AutomaticLock), new FrameworkPropertyMetadata
{
PropertyChangedCallback = OnLockingStateChanged,
});
// AutomaticLock.DoLock: Set on container to cause all children with AutomaticLock.Enabled to lock
public static bool GetDoLock(DependencyObject obj) { return (bool)obj.GetValue(DoLockProperty); }
public static void SetDoLock(DependencyObject obj, bool value) { obj.SetValue(DoLockProperty, value); }
public static readonly DependencyProperty DoLockProperty = DependencyProperty.RegisterAttached("DoLock", typeof(bool), typeof(ControlTemplate), new FrameworkPropertyMetadata
{
Inherits = true,
PropertyChangedCallback = OnLockingStateChanged,
});
// CurrentLock: Used internally to maintain lock state
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public static AutomaticLock GetCurrentLock(DependencyObject obj) { return (AutomaticLock)obj.GetValue(CurrentLockProperty); }
public static void SetCurrentLock(DependencyObject obj, AutomaticLock value) { obj.SetValue(CurrentLockProperty, value); }
public static readonly DependencyProperty CurrentLockProperty = DependencyProperty.RegisterAttached("CurrentLock", typeof(AutomaticLock), typeof(AutomaticLock));
static void OnLockingStateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
AutomaticLock current = GetCurrentLock(obj);
bool shouldLock = GetDoLock(obj) && (GetEnabled(obj) || GetLockTemplate(obj)!=null);
if(shouldLock && current==null)
{
if(!(obj is Control)) throw new InvalidOperationException("AutomaticLock can only be used on objects derived from Control");
new AutomaticLock((Control)obj).Attach();
}
else if(!shouldLock && current!=null)
current.Detach();
}
AutomaticLock(Control target)
{
_target = target;
}
void Attach()
{
_originalTemplate = _target.Template;
_target.Template = GetLockTemplate(_target) ?? SelectDefaultLockTemplate();
SetCurrentLock(_target, this);
}
void Detach()
{
_target.Template = _originalTemplate;
_originalTemplate = null;
SetCurrentLock(_target, null);
}
ControlTemplate SelectDefaultLockTemplate()
{
for(Type type = _target.GetType(); type!=typeof(object); type = type.BaseType)
{
ControlTemplate result =
_target.TryFindResource(new ComponentResourceKey(type, "AutomaticLockTemplate")) as ControlTemplate ??
_target.TryFindResource(new ComponentResourceKey(typeof(AutomaticLock), type.Name)) as ControlTemplate;
if(result!=null) return result;
}
return null;
}
}
此代码将允许您在逐个控件的基础上指定自动锁定模板,或者它将允许您使用在包含 AutomaticLock 类的程序集中定义的默认模板,在包含您的自定义控件的程序集中定义锁定模板适用于,在您的可视化树中的本地资源(包括您的应用程序资源)
如何定义 AutomaticLock 模板
WPF 标准控件的默认模板在合并到 Themes/Generic.xaml 的 ResourceDictionary 中定义在包含 AutomaticLock 类的程序集中。例如,此模板在锁定时会导致所有 TextBoxes 变成 TextBlocks:
<ControlTemplate TargetType="{x:Type TextBox}"
x:Key="{ComponentResourceKey ResourceId=TextBox, TypeInTargetAssembly={x:Type lc:AutomaticLock}}">
<TextBlock Text="{TemplateBinding Text}" />
</ControlTemplate>
自定义控件的默认模板可以在包含自定义控件的程序集中在合并到其 Themes/Generic.xaml 中的 ResourceDictionary 中定义。在这种情况下 ComponentResourceKey 是不同的,例如:
<ControlTemplate TargetType="{x:Type prefix:MyType}"
x:Key="{ComponentResourceKey ResourceId=AutomaticLockTemplate, TypeInTargetAssembly={x:Type prefix:MyType}}">
...
如果应用程序想要覆盖特定类型的标准 AutomaticLock 模板,它可以将自动锁定模板放置在其 App.xaml、Window XAML、UserControl XAML 或单个控件的 ResourceDictionary 中。在每种情况下,ComponentResourceKey 的指定方式都应与自定义控件相同:
x:Key="{ComponentResourceKey ResourceId=AutomaticLockTemplate, TypeInTargetAssembly={x:Type prefix:MyType}}"
最后,可以通过设置AutomaticLock.LockTemplate 属性将自动锁定模板应用于单个控件。
如何在 UI 中使用 AutomaticLock
使用自动锁定:
- 在任何应自动锁定的控件上设置 AutomaticLock.Enabled="True"。这可以以样式或直接在单个控件上完成。它启用对控件的锁定,但不会导致控件实际锁定。
- 当您想要锁定时,只要您希望自动锁定实际发生,就在您的顶级控件(窗口、视图、用户控件等)上设置 AutomaticLock.DoLock="True"。您可以将 AutomaticLock.DoLock 绑定到复选框或菜单项,也可以在代码中对其进行控制。
在查看和编辑模式之间有效切换的一些技巧
这个 AutomaticLock 类非常适合在视图和编辑模式之间切换,即使它们有很大的不同。我有几种不同的技术来构建我的视图以适应编辑时的布局差异。其中一些是:
根据具体情况,通过将其 Template 或 AutomaticLockTemplate 设置为空模板,使控件在编辑或查看模式下不可见。例如,假设“年龄”在视图模式下位于布局的顶部,而在编辑模式下位于底部。在两个地方为“年龄”添加一个文本框。在顶部一个设置模板为空模板,因此它不会在编辑模式下显示。在底部将 AutomaticLockTemplate 设置为空模板。现在一次只能看到一个。
使用 ContentControl 替换内容周围的边框、布局面板、按钮等,而不影响内容。 ContentControl 的模板具有用于编辑模式的周围边框、面板、按钮等。它还有一个具有查看模式版本的 AutomaticLockTemplate。
使用控件替换视图的矩形部分。 (我实际上是指“Control”类的对象,而不是其子类。)同样,您将编辑模式版本放在模板中,将查看模式版本放在 AutomaticLockTemplate 中。
使用具有额外自动调整大小的行和列的网格。使用 AutomaticLock.DoLock 属性上的触发器来更新 Grid 中项目的 Row、Column、RowSpan 和 ColumnSpan 属性。例如,您可以通过将 Grid.Row 从 6 更改为 0 来将包含“年龄”控件的面板移动到顶部。
触发 DoLock 以将 LayoutTranform 或 RenderTransform 应用于您的项目,或设置其他属性,如宽度和高度。如果您希望在编辑模式下使内容更大,或者如果您想让 TextBox 更宽并将其旁边的按钮移到边缘,这将非常有用。
请注意,您可以对整个视图使用选项 #3(具有单独的编辑和查看模式模板的 Control 对象)。如果编辑和查看模式完全不同,就会这样做。在这种情况下,AutomaticLock 仍然为您提供了手动设置两个模板的便利。它看起来像这样:
<Control>
<Control.Template>
<ControlTemplate>
<!-- Edit mode view here -->
</ControlTemplate>
</Control.Template>
<lib:AutomaticLock.LockTemplate>
<ControlTemplate>
<!-- View mode view here -->
</ControlTemplate>
</lib:AutomaticLock.LockTemplate>
</Control>
一般来说,在编辑和查看模式之间调整一些小位置和东西会更容易,并且更好地为您的用户体验,因为用户将拥有一致的布局,但如果您确实需要一个完整的替换 AutomaticLock 为您提供这样的功能嗯。