gpt4 book ai didi

c# - 在普通的 ContentTemplate 中使用 AdornedElementPlaceholder

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:13 25 4
gpt4 key购买 nike

我正在尝试为我的 TextBox 实现一个模板,它在 TextBox 的右侧显示一个小图像,就像它可以在 ValidationTemplates 中实现:

<ControlTemplate x:Key="TextBoxTemplate">
<DockPanel>
<Grid x:Name="image" DockPanel.Dock="Right" Margin="3,0,0,0" Width="20" Height="20">
<Ellipse Width="20" Height="20" Fill="Red" />
<TextBlock Text="!" VerticalAlignment="Top" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" FontSize="16" TextAlignment="Center" Margin="7,-1" />
</Grid>
<AdornedElementPlaceholder />
</DockPanel>
</ControlTemplate>

但是当我尝试将 TextBox.Template 属性绑定(bind)到此 StaticResource 时:

<TextBox Template="{StaticResource TextBoxTemplate}" Text="Test">

它不显示 TextBox 本身。

我通过在 ControlTemplate 中放置另一个 TextBox 而不是 AdornedElementPlaceholder 并将不同的值(文本、样式等)绑定(bind)到 TemplatedParent 找到了解决方法:

<ControlTemplate x:Key="TextBoxTemplate">
<DockPanel DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<Grid x:Name="image" DockPanel.Dock="Right" Margin="3,0,0,0" Width="20" Height="20">
<Ellipse Width="20" Height="20" Fill="Red" />
<TextBlock Text="!" VerticalAlignment="Top" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" FontSize="16" TextAlignment="Center" Margin="7,-1" />
</Grid>
<TextBox Text="{Binding Text}" Style="{Binding Style}" Width="{Binding Width}" Height="{Binding Height}" />
</DockPanel>
</ControlTemplate>

但这是一种相当丑陋的方法,因为您必须显式绑定(bind)每个属性。

还有其他更简单的方法吗?ValidationTemplate中的AdornedElementPlaceholder是怎么做到的?我不能将它用于我的 ContentTemplate 吗?

干杯,亚洛基姆

最佳答案

您的错误是 AdornedElementPlaceholderValidation.ErrorTemplate 一起使用。在这种情况下,它将获取原始控件并将其放在 AdornedElementPlaceholder 的位置。

如果你使用Template,那么没有什么会自动取得原始控制权,你应该定义完整的模板。

如果你想简单地将几个控件组合成一个,我建议创建一个普通的 UserControl,这比处理完整的模板要简单得多。

关于c# - 在普通的 ContentTemplate 中使用 AdornedElementPlaceholder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26631567/

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