如何为文本框标题添加样式?

3

我正在使用如下的 TextBox

<TextBox x:Name="TxtName" IsEnabled="False" Header="Textbox header" IsReadOnly="True" TextWrapping="Wrap"  Text="{Binding Name,Mode=TwoWay}" Style="{StaticResource MyTextBoxStyle}" />

我希望能够在ResourceDictionary中设置TextBoxHeader样式(例如设置字体)。当我设置TextBoxFontFamily属性时,其头部也会受到影响,但实际上我希望可以为TextBox中的文本和Header中的文本分别设置不同的字体风格。
以下是我对TextBox的样式设置:
<Style x:Key="MyTextBoxStyle" TargetType="TextBox">
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="FontFamily" Value="Comic Sans MS" />
</Style>

我该怎么做呢?

1个回答

7

试试这个

     <TextBox Height="70" Width="200" Text="Text" Foreground="Green" FontFamily="Tahoma">
        <TextBox.Header>
            <TextBlock  Text="Header" Foreground="red" FontFamily="Times New Roman"></TextBlock>
        </TextBox.Header>
    </TextBox>

更新

<Page.Resources>
    <Style TargetType="TextBox">
        <Setter Property="Height" Value="70"></Setter>
        <Setter Property="Width" Value="200"></Setter>
        <Setter Property="Text" Value="Text"></Setter>
        <Setter Property="FontFamily" Value="Tahoma"></Setter>
        <Setter Property="Foreground" Value="Green"></Setter>
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Foreground="Red"  Text="{Binding}" FontFamily="Times New Roman"></TextBlock>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

 <TextBox Header="Header" />

但是我该如何在我的TextBox样式中实现这一点呢?我不想为每个TextBox都设置多次。 - ThomasCle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接