Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Style BasedOn not quite working... Any idea why not?

$
0
0

Hi all,

I have a style that has 2 triggers based on an IsEnabled property (1 for true & 1 for false) to enable controls (so we can be in view or edit mode).  It also has a style for Validation.HasError (again 1 for true & 1 for false.)

This works just fine for my standard data entry textboxes.

I want to have a textbox style for required values that works the same way except that when all is good and we are in data entry mode, it uses different colors (so the user can see at a glance that it's required.)  When in an error state or view state, it should use the same colors as the other textboxes.  So the only time it needs to be different is when the user is in edit mode and the data is valid.

So it looks like this:

<Style TargetType="TextBox"
	x:Key="myapp.controls-textbox-dataentry-required"
	BasedOn="{StaticResource myapp.controls-textbox-dataentry}"><Style.Triggers><Trigger Property="IsEnabled"					Value="true"><Setter Property="BorderBrush"
			Value="{DynamicResource myapp.application-requiredBorderBrush}" /><Setter Property="Background"
			Value="{DynamicResource myapp.application-requiredBackgroundBrush}" /><Setter Property="Foreground"
			Value="{DynamicResource myapp.application-requiredForegroundBrush}" /></Trigger></Style.Triggers></Style>

This works as expected Except when there is an error state (HasErrors = true).  Then it doesn't use the errors colors.  If I change it back to the myapp.controls-textbox-dataentry style, it works fine.  And if the data is valid, it works fine.

The interesting thing is that I also have a ControlTemplate that is used to add an "*" to the textbox as part of the validation and that shows up just fine as well.  So I know the validations are fireing.

I could just copy the Validation.HasError setters down to the required style, but that leaves me open for UI errors if I have to change the colors and forget to change one or the other.  And I thought that BasedOn should inherit all the settings defined in the parent.

I am guessing I am missing something obvious, but not quite sure what it may be.

Here is the source for the parent style:

<Style TargetType="TextBox"
				x:Key="myapp.controls-textbox-dataentry"
				BasedOn="{StaticResource myapp-controls-textbox}"><Setter Property="IsEnabled"
					Value="{Binding inEditMode}" /><Setter Property="BorderBrush"
					Value="{DynamicResource myapp.Framework-Application-BackgroundBrush3}" /><Setter Property="Background"
					Value="{DynamicResource myapp.Framework-Application-BackgroundBrush1}" /><Setter Property="Foreground"
					Value="{DynamicResource myapp.Framework-Application-ForegroundBrush1}" /><Setter Property="Validation.ErrorTemplate"
					Value="{StaticResource validationTemplate}" /><Style.Triggers><Trigger Property="IsEnabled"
							Value="true"><Setter Property="BorderBrush"
							Value="{DynamicResource myapp.application-editableBorderBrush}" /><Setter Property="Background"
							Value="{DynamicResource myapp.application-editableBackgroundBrush}" /><Setter Property="Foreground"
							Value="{DynamicResource myapp.application-editableForegroundBrush}" /></Trigger><Trigger Property="IsEnabled"
							Value="false"><Setter Property="BorderBrush"
							Value="{DynamicResource myapp.application-disabledBorderBrush}" /><Setter Property="Background"
							Value="{DynamicResource myapp.application-disabledBackgroundBrush}" /><Setter Property="Foreground"
							Value="{DynamicResource myapp.application-disabledForegroundBrush}" /></Trigger><Trigger Property="Validation.HasError"
							Value="true"><Setter Property="BorderBrush"
							Value="{DynamicResource myapp.application-errorBorderBrush}" /><Setter Property="Tag"
							Value="{Binding RelativeSource={x:Static RelativeSource.Self},
          Path=ToolTip}" /><Setter Property="ToolTip"
							Value="{Binding RelativeSource={x:Static RelativeSource.Self},
          Path=(Validation.Errors)[0].ErrorContent}" /></Trigger><Trigger Property="Validation.HasError"
							Value="false"><!--<Setter Property="BorderBrush"
							Value="{DynamicResource myapp.Framework-Application-BackgroundBrush3}" />--><Setter Property="ToolTip"
							Value="{Binding RelativeSource={x:Static RelativeSource.Self},
          Path=Tag}" /></Trigger></Style.Triggers></Style>
All help is appreciated!

me (and yes, I DO mark correct answers)


Viewing all articles
Browse latest Browse all 18858

Trending Articles