ControlTemplate.Triggers 中的 TemplatedParent 绑定

发布于 2024-08-18 10:16:31 字数 1114 浏览 2 评论 0原文

当其文本为空时,我尝试使用 ControlTemplate 中的触发器将自定义控件的背景设置为视觉画笔。下面显示了相关代码:

<ControlTemplate.Triggers>
  <Trigger Property="Text" Value="">
    <Setter TargetName="MyBorder" Property="Background">
      <Setter.Value>
        <VisualBrush Opacity="0.4" Stretch="None" TileMode="None">
          <VisualBrush.Visual>
            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BackgroundText}" />
          </VisualBrush.Visual>
        </VisualBrush>
      </Setter.Value>
    </Setter>
  </Trigger>
</ControlTemplate.Triggers>

但是,当文本为空时,不会应用视觉画笔。但是,如果我在代码中创建可视画笔并将其公开为依赖属性,则以下代码确实可以工作:

<ControlTemplate.Triggers>
  <Trigger Property="Text" Value="">
    <Setter TargetName="MyBorder" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BackgroundBrush}" />
    </Setter>
  </Trigger>
</ControlTemplate.Triggers>

不过,我宁愿在 XAML 中定义画笔。为什么第二个绑定工作正常,而第一个绑定工作不正确?

When its text is empty, I am attempting to set the background of a custom control to a visual brush using a trigger in the ControlTemplate. The following shows the relevant code:

<ControlTemplate.Triggers>
  <Trigger Property="Text" Value="">
    <Setter TargetName="MyBorder" Property="Background">
      <Setter.Value>
        <VisualBrush Opacity="0.4" Stretch="None" TileMode="None">
          <VisualBrush.Visual>
            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BackgroundText}" />
          </VisualBrush.Visual>
        </VisualBrush>
      </Setter.Value>
    </Setter>
  </Trigger>
</ControlTemplate.Triggers>

When the text is empty, however, the visual brush is not applied. However, if I create the visual brush in code and expose it as a dependency property, the following code does work:

<ControlTemplate.Triggers>
  <Trigger Property="Text" Value="">
    <Setter TargetName="MyBorder" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BackgroundBrush}" />
    </Setter>
  </Trigger>
</ControlTemplate.Triggers>

I would rather define the brush in XAML, though. Why does the second binding work correctly but not the first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-08-25 10:16:31

您是否想创建水印文本框?如果是这样,我通过向自定义控件添加 TextBlock 来创建我的控件,然后在触发器定义中引用它。当TextBox没有焦点,并且控件中没有文本时,水印(TextBlock)将可见。一旦 TexBox 获得焦点,水印就会被隐藏。然后,水印的文本将绑定到您的BackgroundText 属性。

<ControlTemplate.Triggers>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsFocused" Value="false"/>
            <Condition Property="Text" Value="{x:Null}"/>
        </MultiTrigger.Conditions>
        <Setter TargetName="Watermark" Property="Visibility" Value="Visible"/>
    </MultiTrigger>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsFocused" Value="false"/>
            <Condition Property="Text" Value=""/>
        </MultiTrigger.Conditions>
        <Setter TargetName="Watermark" Property="Visibility" Value="Visible"/>
    </MultiTrigger>
</ControlTemplate.Triggers>

Are you trying to create a watermark TextBox? If so, I created mine by adding a TextBlock to the custom control, and then referenced that in the Trigger definitions. When the TextBox does not have the focus, and there is no text in the control, the Watermark (TextBlock) will be visible. Once the TexBox has the focus, the Watermark will be hidden. The text of the Watermark would then be bound to your BackgroundText property.

<ControlTemplate.Triggers>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsFocused" Value="false"/>
            <Condition Property="Text" Value="{x:Null}"/>
        </MultiTrigger.Conditions>
        <Setter TargetName="Watermark" Property="Visibility" Value="Visible"/>
    </MultiTrigger>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsFocused" Value="false"/>
            <Condition Property="Text" Value=""/>
        </MultiTrigger.Conditions>
        <Setter TargetName="Watermark" Property="Visibility" Value="Visible"/>
    </MultiTrigger>
</ControlTemplate.Triggers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文