gpt4 book ai didi

.net - 在 Label 的内容中明确使用 TextBlock 会使 ContentPresenter 行为异常

转载 作者:行者123 更新时间:2023-12-04 23:18:58 27 4
gpt4 key购买 nike

我有一个带有双 ContentPresenter 的自定义 ControlTemplate。该模板应用于 Label。当我将“Random octopus”(只是文本)设置为标签的内容时,它完全按预期工作。当我将“ Random octopus ”设置为 Content 时,它不起作用(只有一个 ContentPresenter 在视觉上表示)。我使用以下代码重现该行为:

<Window x:Class="WeirdTextBlock.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Window.Resources>
<Style TargetType="Label">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border BorderBrush="Red" BorderThickness="1" Padding="2">
<Grid>
<ContentPresenter />
<ContentPresenter Margin="2,2,0,0" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>

<Grid Margin="20" HorizontalAlignment="Left">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="20" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Label Grid.Row="0">
Random octopus
</Label>

<Label Grid.Row="2">
<TextBlock>Random octopus</TextBlock>
</Label>
</Grid>
</Window>

在这里你可以看到它的样子:

我以为when you enter just text into Content property, it gets wrapped by TextBlock ,那么为什么第二个标签的视觉表现与第一个不同呢?以及如何使第二个标签的行为正确(使其看起来像第一个标签,但只能通过修改模板)?谢谢!

最佳答案

这里的根本问题是您试图同时在两个不同的位置放置视觉对象(TextBlock)。一个视觉对象只能有一个父级,因此其中一个内容呈现器“获胜”,而另一个将没有任何内容。

如果您只想拥有 TextBlock 的可视副本,那么请使用 VisualBrush .

关于.net - 在 Label 的内容中明确使用 TextBlock 会使 ContentPresenter 行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5134381/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com