J'utilise .NET Framework 4.5 et j'ai le problème suivant.WPF Adorner ne fonctionne pas si ajouté dans Window.Resources
Si j'ajoute validation ErrorTemplate comme ça, mon Adorner travaillera et spectacle infobulle et le cercle rouge à côté de mon TextBox très bien:
// THIS IS WORKING FINE BUT ONLY FOR txtAge TextBox
<TextBox x:Name="txtAge"
Validation.Error="Validation_Error"
Text="{Binding UpdateSourceTrigger=LostFocus, Path=Age, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
HorizontalAlignment="Left" MaxLength="3" Width="50">
<Validation.ErrorTemplate>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white"/>
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
<Border BorderBrush="red" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</TextBox>
Ainsi, le modèle de validation est ajouté au-dessus dans les <TextBox>
balises pour mon TextBox txtAge et s'applique donc uniquement à ce TextBox.
Cependant, je voudrais avoir un style qui s'applique à tous les TextBox, donc j'ajoute l'Adorner à l'intérieur des balises <Window.Resources>
. Mais cela ne montre l'info-bulle, ni le cercle rouge:
// I WANT TO MAKE IT APPLY TO ALL TEXTBOXES BUT THIS IS NOT WORKING
<Window.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Margin" Value="5,0,5,0" />
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="0,2,40,2" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white"/>
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
<Border BorderBrush="red" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
Pourquoi la 1ère et 2ème travaille est pas? Je suis nouveau à WPF.
Avez-vous essayé de créer une ressource distincte pour le modèle d'erreur, comme controltemplate x: Key = "errorTemplate", et l'utiliser dans le style TextBox? Les autres propriétés de TextBox s'appliquent-elles correctement, par ex. la marge? – SnowballTwo
@SnowballTwo Comment créer une ressource séparée? Je suis nouveau à cela. Thansk – pixel
Mettez simplement ControlTemplate en tant qu'enfant dans votre noeud Window.Resources et ajoutez l'attribut x: Key. Vous pouvez vous référer au modèle en écrivant {StaticResource YourTemplateKey} – SnowballTwo