Filippo
Filippo

Reputation: 1161

How to modify legacy named style for having different setters based on targetTypes?

I have this named style

<Style x:Key="validationSupport" TargetType="{x:Type Control}">
    <Setter Property="Margin" Value="5,2,14,2" />
    ...OMISSIS...
    <Style.Triggers>
        ...OMISSIS...
        <DataTrigger Binding="{Binding DataContext.ActiveWorkspace.Editable, RelativeSource={RelativeSource  AncestorType=Window}}" Value="False">
            <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
    </Style.Triggers>

</Style>

I use it extensively for TextBoxes, ComboBoxes, DatePickers etc, so I used as TargetType a super class for all these elements, Control.

Now I would like to differentiate the setter inside the dataTrigger using specific properties that 'Control' doesn't have. It seems I have to create different styles with different names,each for every targetType I want to differentiate, but that way I have to change the style name inside all elements which use it. Is there a smarter way to achieve that goal ? I don't want want to go and modify every xaml file I have.

Update after first answer

I have tried to put the following setters inside the datatrigger:

<Setter Property="Background" Value="#FFECECF8" />
<Setter Property="CheckBox.IsEnabled" Value="False" />
<Setter Property="DatePicker.IsEnabled" Value="False" />
<Setter Property="ComboBox.IsEnabled" Value="False" />
<Setter Property="TextBox.IsReadOnly" Value="True" /> 

Unfortunately the tests gave odd results. The IsEnabled property is set for TextBoxes too despite the prefix should limit its application to CheckBoxes, DatePickers and ComboBoxes.

My final need was to make some control contents unchangeable avoiding the difficult to read colors associated with disabled controls. From previous researches I understood that changing the colors for a 'disabled' control is not an easy task and involves the redefinition of the control template. So I thought to apply a combination of IsReadOnly and Background, but it is not applicable for the above problem. In fact CheckBoxes, DatePickers and ComboBoxes can only be made unchangeable using the IsEnabled property. Am I missing something ?

Upvotes: 1

Views: 105

Answers (1)

XAMeLi
XAMeLi

Reputation: 6289

There is a way, but I have to warn you - this is far from best-practice and should be avoided

WPF allows you to use desired type as a prefix for the property. That way, if you apply the style to a control that doesn't inherit from the prefixed type - the setter is ignored.

<Style x:Key="validationSupport" TargetType="{x:Type Control}">
    <Setter Property="Margin" Value="5,2,14,2" />
    ...OMISSIS...
    <Style.Triggers>
        ...OMISSIS...
        <DataTrigger Binding="{Binding DataContext.ActiveWorkspace.Editable, RelativeSource={RelativeSource  AncestorType=Window}}" Value="False">
            <Setter Property="IsEnabled" Value="False" />
            <Setter Property="Button.Background" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>

[Test this extensively, since I suspect that it might create memory leaks.]

Upvotes: 0

Related Questions