Dot-Net

錯誤模板顯示在其他控制項上方,應隱藏時

  • December 2, 2014

我正在嘗試使用該IDataErrorInfo介面在我的 WPF 應用程序中實現驗證,但我遇到了一個不太理想的情況。

我有這個模板,當控制項無法驗證時使用

<ControlTemplate x:Key="errorTemplate">
   <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>

一切都很好,直到我嘗試在未通過驗證的控制項上方顯示某些內容,例如在其上方顯示停靠項:

正常顯示 部分控制項隱藏時顯示

我怎樣才能避免這種情況並讓我的錯誤模板顯示在停靠項下方,因為它應該?

編輯

我發現我可以TextBox用 anAdornerDecorator來解決這個問題,但我真的不想為TextBox我的應用程序中的每個控制項都這樣做。有沒有辦法用aStyle或其他方式設置它?

編輯 2

我可能會更改預設TextBoxControlTemplate 以包含AdornerDecorator,但我不太熱衷於更改 WPF 的任何預設控制項模板。歡迎任何其他建議。

好的,我找到了一個相對簡單的解決方案,它不會強迫我更改任何控制項模板。

TextBox而不是AdornerDecorator像這樣裝飾每個

<StackPanel>
   <AdornerDecorator>
       <TextBox Text={Binding ...} />
   </AdornerDecorator>
   <AdornerDecorator>
       <TextBox Text={Binding ...} />
   </AdornerDecorator>
</StackPanel>

我可以AdornerDecorator將整個視圖包裹起來,從而達到相同的效果。

<AdornerDecorator>
   <StackPanel>
       <TextBox Text={Binding ...} />
       <TextBox Text={Binding ...} />
   </StackPanel>
</AdornerDecorator>

這樣我可以在每個視圖中最多定義一次。

引用自:https://stackoverflow.com/questions/10294498