隐藏在validationTemplate中的AdornedElementPlaceholder

本文关键字:AdornedElementPlaceholder 中的 validationTemplate 隐藏 | 更新日期: 2023-09-27 18:01:30

尊敬的stackoverflow的人,为什么我的文本框,这是我的有效控制,隐藏在DockPanel背景在这个模板?

    <ControlTemplate x:Key="validationTemplate">
        <DockPanel Background="Black">
            <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
            <AdornedElementPlaceholder/>
        </DockPanel>
    </ControlTemplate>

如果背景设置为"透明",文本框是可见的,但我不能点击里面(光标不会改变)。

我如何设置背景为我的模板没有隐藏我的adrendelement占位符?

由于gpx

隐藏在validationTemplate中的AdornedElementPlaceholder

装饰层确实位于元素的顶部,并且可以拦截鼠标交互。在你的例子中,通过将背景应用到DockPanel,你向WPF表明该对象具有"HitTestVisible"区域,并将拦截鼠标点击。

另一个令人困惑的注意事项是"Transparent"仍然是HitTestVisible。如果你不想让它拦截鼠标点击,那么你应该将背景设置为"{x:Null}"或留空。

两个选择:

  1. 设置Background="{x:Null}"。这基本上是没有背景和防止鼠标命中测试。
  2. 在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>
相关文章:
  • 没有找到相关文章