在 Validation.ErrorTemplate 中绑定 TextBlock 的 FontSize
我为 TextBox 声明了一个简单的 Validation.ErrorTemplate,如下所示。
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock Text="!" DockPanel.Dock="Right"
FontSize="{TemplateBinding TextBox.FontSize}"
Foreground="Red"/>
<AdornedElementPlaceholder Name="adornerPlaceholder" />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我希望感叹号的字体大小与 TextBox 的 font(edited) 大小相同,但它并没有达到预期的结果,并且始终获得默认字体大小。此外,我尝试使用 RelativeSource={RelativeSource Mode=TemplatedParent}, Path=FontSize
进行绑定,但也无法解决问题。为什么会出现这种情况呢?如何使感叹号的大小与 TextBox 的大小相同?
I declared a simple Validation.ErrorTemplate for TextBox as the following.
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock Text="!" DockPanel.Dock="Right"
FontSize="{TemplateBinding TextBox.FontSize}"
Foreground="Red"/>
<AdornedElementPlaceholder Name="adornerPlaceholder" />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I expect that the font size of the exclamation mark will be the same font(edited) size as TextBox, but it doesn't result in the expectation and always gets the default font size. Furthermore, I tried Binding using RelativeSource={RelativeSource Mode=TemplatedParent}, Path=FontSize
, but it also cannot solve the problem. Why is this situation occurred? How can I make the the exclamation mark gets the same size as TextBox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不绑定到
AdornedElementPlaceholder
?这未经测试,但应该有效:)
Why don't you bind to the
AdornedElementPlaceholder
?This is untested, but it should work :)
另一种选择是将
TextBlock
包装在Viewbox
中,它会随着装饰元素自动缩放其高度:这将适用于任何被装饰的元素,无论字体大小如何,any 用于任何感叹号图形(即文本、路径、元素等)
可以使用边距调整定位/布局。
Another option is to wrap the
TextBlock
in aViewbox
, which automatically scales its height along with the adorned element:This would then work for any element being adorned, regardless of font size, any for any exclamation graphic (i.e. text, path, element, etc)
Positioning/layout can be tweaked with margin.