问题
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?
回答1:
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).
回答2:
What about if you set a default value for 'local:IMyControl.Background' in the Style also, like:
<Style x:Key="ControlRellenableEstilo">
<Setter Property="local:IMyControl.Background" Value="Gray" />
<!--or use TemplateBinding to Parent to get suitable color-->
<Style.Triggers>
... skipped ...
</Style.Triggers>
</Style>
Also check a Precedence List on your CustomControl.
来源:https://stackoverflow.com/questions/7330680/common-style-for-different-controls-with-the-same-custom-properties