2015-12-17 19 views
0

我正在使用.NET Framework 4.5,並且出現以下問題。如果在Window.Resources中添加WPF Adorner不起作用

如果我添加驗證ErrorTemplate這個樣子,我的裝飾器將運行,顯示工具提示和紅色圓圈旁邊我的文本框只是罰款:

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

因此,驗證模板上面是<TextBox>標籤中添加了對我的TextBox txtAge,因此僅適用於該TextBox。

但是,我想要一個適用於所有文本框的樣式,所以我在<Window.Resources>標籤中添加了Adorner。但是,這將不能顯示工具提示,也沒有紅圈:

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

爲什麼一日一工作和第二一個是不是?我是WPF的新手。

+0

您是否嘗試過爲錯誤模板創建一個單獨的資源,如controltemplate x:Key =「errorTemplate」,並在Textbox樣式中使用它? TextBox的其他屬性是否正確應用?保證金? – SnowballTwo

+0

@SnowballTwo如何創建單獨的資源?我是新來的。 Thansk – pixel

+1

只需將ControlTemplate作爲子項放置在Window.Resources節點中並添加x:Key屬性即可。你可以通過編寫{StaticResource YourTemplateKey} – SnowballTwo

回答

0

基於@SnowballTwo的答案,我想通了。

移動代碼到Windows.Resources部分,並將其添加X:主要象下面這樣:

<ControlTemplate x:Key="ValidationTemplate"> 
    <DockPanel LastChildFill="true"> 
     <Border Background="Red" DockPanel.Dock="right" 
          Margin="5,0,0,0" Width="10" Height="10" 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="{StaticResource ValidationTemplate}"