2009-09-25 27 views
10

alt text http://img42.imageshack.us/img42/4161/blinkthru.pngWPF - Làm thế nào tôi có thể đặt một usercontrol qua một AdornedElementPlaceholder?

Tôi đang cố gắng xác thực để không hiển thị thông qua hộp thoại phương thức tùy chỉnh của mình. Tôi đã thử thiết lập zindex của hộp thoại và và của các phần tử trong mẫu này. Bất kỳ ý tưởng?

này được xuất phát từ một mẫu xác nhận:

<ControlTemplate x:Key="ValidationTemplate"> 
     <DockPanel> 
      <TextBlock Foreground="Red" FontSize="20" Panel.ZIndex="-10">!</TextBlock> 
      <Border Name="validationBorder" BorderBrush="Red" BorderThickness="2" Padding="1" CornerRadius="3" Panel.ZIndex="-10"> 
       <Border.Resources> 
        <Storyboard x:Key="_blink"> 
         <ColorAnimationUsingKeyFrames AutoReverse="True" BeginTime="00:00:00" Storyboard.TargetName="validationBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" RepeatBehavior="Forever"> 
          <SplineColorKeyFrame KeyTime="00:00:1" Value="#00FF0000"/> 
         </ColorAnimationUsingKeyFrames> 
        </Storyboard> 
       </Border.Resources> 
       <Border.Triggers> 
        <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
         <BeginStoryboard Storyboard="{StaticResource _blink}" /> 
        </EventTrigger> 
       </Border.Triggers> 
       <AdornedElementPlaceholder/> 
      </Border> 
     </DockPanel> 
    </ControlTemplate> 

Hộp thoại:

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    x:Class="GunMiser.Controls.PendingChangesConfirmationDialog" 
    Height="768" Width="1024" mc:Ignorable="d"> 
    <Grid Background="White"> 
     <Rectangle x:Name="MainRectangle" Margin="0,0,0,0" Style="{DynamicResource UserControlOverlayRectangleStyle}" Opacity="0.85"/> 
     <Border Margin="288,250,278,288" Background="#FF868686" BorderBrush="Black" BorderThickness="1"> 
      <Border.Effect> 
       <DropShadowEffect Color="#FFB6B2B2"/> 
      </Border.Effect> 
      <TextBlock x:Name="textBlockMessage" Margin="7,29,7,97" TextWrapping="Wrap" d:LayoutOverrides="VerticalAlignment" TextAlignment="Center"/> 
     </Border> 
     <Button x:Name="OkButton" Click="OkButton_Click" Margin="313,0,0,328" VerticalAlignment="Bottom" Height="24" Content="Save Changes" Style="{DynamicResource GunMiserButtonStyle}" HorizontalAlignment="Left" Width="103"/> 
     <Button Click="CancelButton_Click" Margin="453.294,0,456,328" VerticalAlignment="Bottom" Height="24" Content="Cancel Changes" Style="{DynamicResource GunMiserButtonStyle}"/> 
     <Button Click="CancelActionButton_Click" Margin="0,0,304,328" VerticalAlignment="Bottom" Height="24" Content="Go Back" Style="{DynamicResource GunMiserButtonStyle}" HorizontalAlignment="Right" Width="114.706"/> 
    </Grid> 
</UserControl> 

Và cửa sổ tổng thể là:

<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> 
     <controls:PendingChangesConfirmationDialog x:Name="PendingChangesConfirmationDialog" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" Width="1008" Height="730" Visibility="Collapsed" Panel.ZIndex="100" /> 
     <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> 
</Window> 
+0

http://stackoverflow.com/questions/321327/how-do-i-get-rid-of-the-red-rectangle-when-my-wpf-binding-validation-has-failed- a - Câu trả lời của Donnelle đã giúp tôi giải quyết vấn đề. – Aligned

Trả lời

13

Bạn vấn đề là các mẫu lỗi là hiển thị trong lớp adorner mà trong trường hợp của bạn sẽ được ở cửa sổ gốc.

Nếu bạn thay đổi mã của mình thành mã sau, bạn sẽ không gặp vấn đề gì, bởi vì sẽ có một lớp riêng biệt được tạo ra xung quanh phần còn lại của bạn kiểm soát.

<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> 

+0

Cảm ơn câu trả lời, tôi không có mã để thử (tôi đang làm việc ngoài trang web), nhưng tôi sẽ bình luận khi tôi quay lại với nó (có thể không đến tháng 8 :)). – Aligned

+0

Cảm ơn, tôi đã có cùng một vấn đề và giải pháp này hoạt động rất tốt. –

+0

Cảm ơn! Tôi ước tôi có thể bỏ phiếu 1.000 lần này. – danjarvis

0

Ngoài ra, nếu bạn muốn thay đổi chỉ là phong cách của phần tử (nền của nó, tiền cảnh, vv) và không đó là toàn bộ ControlTemplate (trong đó bao gồm các adorner), bạn có thể thiết lập Validation. ErrorTemplate sao cho nó không có đường viền hoặc dấu chấm than, sau đó sử dụng trình kích hoạt kiểu để thay đổi kiểu dựa trên thuộc tính "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> 
Các vấn đề liên quan