样式中枚举类型的 DP 的设置值正在使设计器崩溃
本文关键字:崩溃 枚举 类型 DP 设置 样式 | 更新日期: 2023-09-27 18:21:28
我创建了自定义的无外观控件,该控件定义了类型为 enum
的DependencyProperty
,并具有一个ToggleButton
,该应该通过Trigger
IsChecked
状态设置此属性。不知何故,当我这样定义Setter
时:
<ControlTemplate.Triggers>
<Trigger SourceName="ToggleButton" Property="IsChecked" Value="True">
<Setter Property="ConnectionMode" Value="{x:Static Controls:ConnectionMode.Mode1}" />
</Trigger>
...
</ControlTemplate.Triggers>
然后,Visual Studio 2013 设计器立即失败并显示错误:
'ArgumentException: 'Mode1' 不是 Setter 上 'MyProject.Controls.CustomControl1.ConnectionMode' 属性的有效值。
at System.Windows.Setter.Seal(( at System.Windows.SetterBaseCollection.Seal(( at System.Windows.TriggerBase.ProcessSettersCollection(SetterBaseCollection setters( at System.Windows.Trigger.Seal(( at System.Windows.TriggerCollection.Seal(( at System.Windows.StyleHelper.SealTemplate(FrameworkTemplate frameworkTemplate, Boolean& isSealed, FrameworkElementFactory templateRoot, TriggerCollection triggers, ResourceDictionary resources, HybridDictionary childIndexFromChildID, FrugalStructList
1& childRecordFromChildIndex, FrugalStructList
1& triggerSourceRecordFromChildIndex, FrugalStructList1& containerDependents, FrugalStructList
1& resourceDependents, ItemStructList'1& eventDependents, HybridDictionary&triggerActions, HybridDictionary&dataTriggerRecordFromBinding, Boolean& hasInstanceValues, EventHandlersStore& eventHandlersStore( at System.Windows.FrameworkTemplate.Seal((
DependencyProperty
定义为:
namespace MyProject.Controls
{
using ...
public enum ConnectionMode
{
Mode1,
Mode2,
}
public class CustomControl1 : ContentControl
{
public static readonly DependencyProperty ConnectionModeProperty = DependencyProperty.Register("ConnectionMode", typeof(ConnectionMode), typeof(CustomControl1), new PropertyMetadata(default(ConnectionMode), OnCustomConnectionModeChanged));
所以我的问题是:如何从Trigger
设置枚举类型的 DP,并且仍然在 Visual Studio 中以"设计模式"看到控件?
属性ConnectionMode
在 CustomControl1
处定义,因此无法从 ControlTemplate 进行设置。属性可以从 CusotmControl1 样式设置,但不能从控件模板设置。
这样的东西会起作用:
<Controls:CustomControl1.Style>
<Style TargetType="{x:Type Controls:CustomControl1}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ConnectionMode"
Value="{x:Static Controls:ConnectionMode.Mode1}"/>
</Trigger>
</Style.Triggers>
</Style>
</Controls:CustomControl1.Style>