Reputation: 216
I am trying to apply a custom style to a control whenever it is in a particular state, this style can be set on the object as a style. However, when setting a trigger to do this, the style property cannot be set again:
<Style TargetType="{x:Type ContentPresenter}">
<Style.Triggers>
<Trigger Property="ContentTemplate" Value="{x:Null}">
<Setter Property="Style" Value="{Binding MouseOverGroupStyle, RelativeSource={RelativeSource TemplatedParent}}" />
</Trigger>
</Style.Triggers>
</Style>
Style object is not allowed to affect the Style property of the object to which it applies.
Which makes sense, however, what is the alternative? I cannot bind to the setters list, because it is readonly.
Upvotes: 2
Views: 787
Reputation: 3764
Your solution here is to use a StyleSelector, which takes the ContentPresenter and checks the ContentTemplate.
internal class ContentTemplateStyleSelector : StyleSelector
{
public Style NullStyle { get; set; }
public Style DefaultStyle { get; set; }
public override Style SelectStyle(object item, DependencyObject container)
{
var cp = container as ContentPresenter;
if (cp == null)
return null;
if (cp.ContentTemplate == null)
return NullStyle;
return DefaultStyle;
}
}
Unfortunately, ContentPresenter doesn't have a StyleSelector property to which you can bind a StaticResource of an instance of your ContentTemplateStyleSelector so you may need to cast from ContentPresenter to something which does.
Alternatively, there is the option of using a DataTemplateSelector.
Upvotes: 2
Reputation: 11844
The Style Object will not be allowed to affect the Style property of the object to which it applies...
Check this in Windows-Presentation-Foundation.com to know more...
Upvotes: 0
Reputation: 128061
You may change the Template property in your style.
Another, maybe better approach would be to use VisualStates.
Upvotes: 1