Aarohi S
Aarohi S

Reputation: 157

Extend Style in wpf

I tried the following way to extend style and add tooltip message if validationresult is true. But it shows error "Property style is set more than once". How can I extend style for this case. Any help will be appreciated. Thanks.

<TextBox Width="500" Style="{StaticResource HasInvalidValue}">
                <TextBox.Text>
                    <Binding Path="Text" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <Validator:PathFormatValidationRule/>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
                <TextBox.Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource HasInvalidValue}">
                    <Style>
                        <Setter Property="ToolTip" Value="Enter Text."/>
                    </Style>
                </TextBox.Style>
            </TextBox>

And the style is defined as:

<Style x:Key="HasInvalidValue" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="Background" Value="White"/>
            <Setter Property="ToolTip"
          Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                          Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

Upvotes: 0

Views: 1105

Answers (1)

eMko
eMko

Reputation: 1143

First is set in

<TextBox Style="{StaticResource HasInvalidValue}" ...
...
</TextBox>

and then

<TextBox.Style ...
</TextBox.Style>

From textbox element remove the style attribute.

Upvotes: 2

Related Questions