XAML - 设置样式后文本未显示在我的文本框中 [英] XAML - Text is not showing in my textbox after I styled it

查看:27
本文介绍了XAML - 设置样式后文本未显示在我的文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在我正在开发的应用程序中设置了文本框的样式,但突然间我看不到任何已绑定到文本框的文本.我觉得我缺少某种 ContentPresenter.无论如何,这是样式.

So, I styled my TextBoxes in an app that I'm working on, and all of a sudden I can't see any text that I've bound to my TextBoxes. I feel like I'm missing some kind of ContentPresenter. Well anyway, here is the styling.

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Height" Value="26"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Border BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Background="{TemplateBinding Background}">
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Value="#FF2F2F2F" Property="Background"/>
                        <Setter Value="White" Property="Foreground"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是我的文本框的设置方式

And here is how my TextBoxes are set up

<TextBox Grid.Row="2" Grid.Column="5" BorderThickness="1" Text="{Binding VariableName}">

有什么想法吗?

推荐答案

您的直觉是对的,您需要一个名为 PART_ContentHost 在您的 ControlTemplate 中:

Your hunch is right, you need to have a template part with the name PART_ContentHost inside your ControlTemplate:

<ControlTemplate TargetType="TextBox">
    <Border BorderThickness="{TemplateBinding BorderThickness}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Background="{TemplateBinding Background}">
        <ScrollViewer Margin="0"
                    x:Name="PART_ContentHost" />
    <!-- ... -->

这是因为 TextBoxControlTemplate 需要名称为 PART_ContentHost 的部分.您可以查看内置控件的 ControlTemplate 示例 找出需要哪些命名模板部件以及每个部件必须能够做什么才能保留正常功能.

This is because the ControlTemplate for a TextBox requires a part with the name PART_ContentHost. You can view the ControlTemplate examples for the built-in controls to find out what named template parts are required and what each part must be able to do in order to retain normal functionality.

这篇关于XAML - 设置样式后文本未显示在我的文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆