Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I am working on XAML user control to display validation errors using a ValidationRule and a data template to display the validation error to the user. I thought putting in a textblock, styling it with red as the foreground color, etc., would have been sufficient. It turns out that I was wrong about that. Apparently, any control within a data template cannot be simply styled. I found this link on Stack Overflow that looked very promising to resolve my issue: WPF Some styles not applied on DataTemplate controls.

What I have tried:

Following the answer that starts with, "I discovered a simple workaround..." to the post on SO I gave, I have this code:

<StackPanel Orientation="Horizontal">
	<StackPanel.Resources>
		<DataTemplate DataType="{x:Type ValidationError}">
			<DataTemplate.Resources>
				<Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}" />
			</DataTemplate.Resources>
			<TextBlock Text="{Binding ErrorContent}" 
					   Foreground="Red"
					   VerticalAlignment="Center"
					   FontWeight="Bold"/>
		</DataTemplate>
	</StackPanel.Resources>
	<TextBox x:Name="MaxProficienciesTextBox"
		 Style="{StaticResource LeftAlignTextBoxStyle}"
		 Margin="0,5,3,5"
		 MaxLength="2"
		 IsEnabled="{Binding IsMaxProficienciesEnabled}" 
		 KeyUp="MaxProficienciesTextBox_KeyUp" Loaded="MaxProficienciesTextBox_Loaded">
		<TextBox.Resources>
			<val:BindingProxy x:Key="proxy" Data="{Binding}" />
		</TextBox.Resources>
		<TextBox.Text>
			<Binding Path="MaxProficiencies"
				 UpdateSourceTrigger="PropertyChanged" ValidatesOnExceptions="True">
				<Binding.ValidationRules>
					<val:MaxProficienciesValidationRule Min="1">
						<val:MaxProficienciesValidationRule.Wrapper>
							<val:MaxProficienciesValidationRuleWrapper SolutionID="{Binding SolutionID}" />
						</val:MaxProficienciesValidationRule.Wrapper>
					</val:MaxProficienciesValidationRule>
				</Binding.ValidationRules>
			</Binding>
		</TextBox.Text>
	</TextBox>
	<ContentPresenter Content="{Binding ElementName=MaxProficienciesTextBox, Path=(Validation.Errors)[0].ErrorContent}" />
</StackPanel>


I'm getting an error on the {x:Type TextBlock} style in the DataTemplate.Resources. I don't understand why. Please show me what I've done wrong.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900