具有相同自定义属性的不同控件的通用样式
我有几个自定义控件,它们具有一些共同的自定义属性,并且我想应用使用这些属性触发的通用样式。我将这些通用属性放在一个接口(IMyControl)中,并让我的控件实现它,但似乎样式不能有一个作为 TargetType 的接口...
我的自定义属性是两个布尔值:bMandatory 和 bIncomplete。常见的样式部分是:
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Background" Value="{StaticResource FocusedBG}" />
</Trigger>
<Trigger Property="bMandatory" Value="True">
<Setter Property="Background" Value="{StaticResource MandatoryBG}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="bMandatory" Value="True" />
<Condition Property="bIncomplete" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource IncompleteBG}" />
</MultiTrigger>
</Style.Triggers>
我还在界面中放入了 IsKeyboardFocusWithin bool 和 Background 画笔,然后尝试使用:
<Style x:Key="ControlRellenableEstilo">
<Style.Triggers>
<Trigger Property="local:IMyControl.IsKeyboardFocusWithin" Value="True">
<Setter Property="local:IMyControl.Background" Value="{StaticResource FocusedBG}" />
</Trigger>
<Trigger Property="local:IMyControl.bMandatory" Value="True">
<Setter Property="local:IMyControl.Background" Value="{StaticResource MandatoryBG}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="local:IMyControl.bMandatory" Value="True" />
<Condition Property="local:IMyControl.bIncomplete" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource IncompleteBG}" />
</MultiTrigger>
</Style.Triggers>
</Style>
构建时它不会抱怨,但是当应用程序执行时,它会抛出一个 XamlParseException ,表示
无法分配给属性“System.Windows.Setter.Value”
指向该行的属性“System.Windows.Setter.Value”:
它有一个内部异常,并显示以下消息:
值不能为空。参数名称:属性'
Value="{StaticResource FocusedBG}" 部分正在我的其余样式中工作...... 知道我该如何解决这个问题吗?
I have several custom controls that have in common some custom properties and I would like to apply a common style that triggers with those properties. I put those common properties in an interface (IMyControl) and made my controls implement it, but it seems that a style can't have an interface as TargetType...
My custom properties are two booleans: bMandatory and bIncomplete. The common style part is:
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Background" Value="{StaticResource FocusedBG}" />
</Trigger>
<Trigger Property="bMandatory" Value="True">
<Setter Property="Background" Value="{StaticResource MandatoryBG}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="bMandatory" Value="True" />
<Condition Property="bIncomplete" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource IncompleteBG}" />
</MultiTrigger>
</Style.Triggers>
I also put in the interface the IsKeyboardFocusWithin bool and the Background brush and then tried with:
<Style x:Key="ControlRellenableEstilo">
<Style.Triggers>
<Trigger Property="local:IMyControl.IsKeyboardFocusWithin" Value="True">
<Setter Property="local:IMyControl.Background" Value="{StaticResource FocusedBG}" />
</Trigger>
<Trigger Property="local:IMyControl.bMandatory" Value="True">
<Setter Property="local:IMyControl.Background" Value="{StaticResource MandatoryBG}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="local:IMyControl.bMandatory" Value="True" />
<Condition Property="local:IMyControl.bIncomplete" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource IncompleteBG}" />
</MultiTrigger>
</Style.Triggers>
</Style>
It doesn't complain when building but, when the application is executed, it throws a XamlParseException that says
Failed to assign to property 'System.Windows.Setter.Value'
pointing at the line:<Setter Property="local:IMyControl.Background" Value="{StaticResource FocusedBG}" />
It has an inner exception with the following message:
Value cannot be null. Parameter name: property'
The Value="{StaticResource FocusedBG}" part is working in the rest of my styles...
Any idea how could I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Setter 仅对 依赖属性 进行操作,在无法注册它们的界面中,而且它们只能注册一次,因此即使您在界面中为它们指定了某些字段,这也可能不是一个好主意。您可能需要考虑使用附加属性或使用基类其上的该属性(这可能不是一个选项)。
Setters operate on dependency properties only, in an interface you cannot register them, and they can only be registered once, so even if you specify some field for them in the interface that probably is far from a good idea. You might want to consider using attached properties or using a base-class with that property on it (which probably is not an option).
如果您还在样式中设置“local:IMyControl.Background”的默认值,例如:
另请检查 优先级列表。
What about if you set a default value for 'local:IMyControl.Background' in the Style also, like:
Also check a Precedence List on your CustomControl.