WPF - How can I place a usercontrol over an AdornedElementPlaceholder?

人盡茶涼 提交于 2019-11-30 12:38:22

You problem is that the error template is shown in the adorner layer which in your case will be at the root window.

If you were to change your code to the following you wouldn't have a problem, because there would be a separate adorner layer created around the rest of you controls.

<Window x:Class="GunMiser.Views.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.codeplex.com/CompositeWPF"
xmlns:controls="clr-namespace:GunMiser.Controls;assembly=GunMiser.Controls"
Title="Gun Miser"
Height="768" Width="1024">
<Canvas>        
    <AdornerDecorator>
        <Canvas>
            <ContentControl x:Name="FilterRegion" cal:RegionManager.RegionName="FilterRegion" Width="326" Height="656" Canvas.Top="32" VerticalAlignment="Top" HorizontalAlignment="Left" />
            <ContentControl Name="WorkspaceRegion" cal:RegionManager.RegionName="WorkspaceRegion" Width="678" Height="726" Canvas.Left="330" VerticalAlignment="Top" HorizontalAlignment="Left"/>
            <Button Click="GunsButton_Click" Width="75" Height="25" Content="Guns" Canvas.Top="3" Style="{DynamicResource GunMiserButtonStyle}"/>
            <Button Click="OpticsButton_Click" Width="75" Height="25" Content="Optics" Canvas.Left="81" Canvas.Top="3" Style="{DynamicResource GunMiserButtonStyle}"/>
            <Button Click="SettingsButton_Click" Width="56" Height="28" Content="Settings" Canvas.Left="944" Style="{DynamicResource GunMiserButtonStyle}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Button Click="AccessoriesButton_Click" Width="75" Height="25" Content="Accessories" Canvas.Left="239" Canvas.Top="3" Style="{DynamicResource GunMiserButtonStyle}"/>
            <Button Click="AmmunitionButton_Click" Width="75" Height="25" Content="Ammunition" Canvas.Left="160" Canvas.Top="3" Style="{DynamicResource GunMiserButtonStyle}"/>
        </Canvas>
    </AdornerDecorator>
    <controls:PendingChangesConfirmationDialog x:Name="PendingChangesConfirmationDialog" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" Width="1008" Height="730" Visibility="Collapsed" Panel.ZIndex="100" />
</Canvas>

Alternatively, if you want to change just the style of the element (it's background, foreground, etc) and not it's whole ControlTemplate (which includes the adorner), you can set the Validation.ErrorTemplate so that it doesn't have a border, or exclamation point, then use a style trigger to change the style based on the property "Validation.HasError".

<Style TargetType="{x:Type TextBlock}" x:Key="TextBlockErrorStyle">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <!-- this gets rid of all adornment INCLUDING THE DEFAULT RED BORDER -->
                <AdornedElementPlaceholder Name="controlWithError" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="Background" Value="MistyRose"/>
            <Setter Property="ToolTip" 
                Value="{Binding RelativeSource={RelativeSource Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>

</Style>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!