裝飾器層不坐在元件的頂部,並且可以攔截鼠標交互。在你的情況下,通過對DockPanel應用背景,你向WPF表明該對象的區域是「HitTestVisible」,並且會攔截鼠標點擊。
另一個令人困惑的說法是「透明」仍然是HitTestVisible。如果您不想攔截鼠標點擊,則應將背景設置爲「{x:Null}」或將其保留爲空。
兩個選項:
- 設置背景= 「{x:NULL}」。這基本上沒有背景,並防止鼠標擊中測試。
- 指定DockPanel上的IsHitTestVisible =「False」。這將允許鼠標交互繞過該圖層並轉到下一個可用圖層。
編輯:
下面是在KaXaml對我工作的例子。只需在文本框中輸入類似「word」的字樣即可生成驗證錯誤。通過將背景顏色設置爲半透明顏色,我可以看到文本框。設置IsHitTestVisible =「False」允許我用鼠標點擊文本框。
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<ControlTemplate x:Key="validationTemplate">
<DockPanel Background="#5000" IsHitTestVisible="False">
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<Style TargetType="TextBox" x:Key="validationStyle">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<StackPanel Name="grd" Width="100" Height="100">
<TextBox
VerticalAlignment="Top"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Text="{Binding ElementName=grd, Path=Width, Mode=TwoWay, ValidatesOnExceptions=True}" />
<TextBox
VerticalAlignment="Top"
Text="{Binding ElementName=grd, Path=Height, Mode=TwoWay, ValidatesOnExceptions=True}"
Style="{StaticResource validationStyle}"
>
</TextBox>
</StackPanel>
</Page>
+1幾周前有完全相同的問題,放棄搜索。出於某種原因,背景會在光標到達texbox之前吞下光標。我試着擺弄z軸設置等,但無法找到解決方案。 – stijn