如何将带有触发器的WPF文本框样式放入Windows资源中

时间:2016-07-22 08:49:26

标签: wpf xaml

我有以下与其关联的样式的WPF文本框。有什么方法可以将 TextBox.Style 推送到资源中,以便可以重用它?

graph.getGridLabelRenderer().setHumanRounding(false);

1 个答案:

答案 0 :(得分:3)

创建一个资源字典,将您的样式放入其中并将其添加到App.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:metroChart="clr-
                    >

    <Style TargetType="TextBox" >
                            <Style.Resources>
                                <VisualBrush x:Key="CueBannerBrush" AlignmentX="Center"  AlignmentY="Center" Stretch="None">
                                    <VisualBrush.Visual>
                                        <Label Content="Camera Ip Address" Foreground="Gray" Opacity="0.5" FontStyle="Italic" />
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </Style.Resources>
                            <Style.Triggers>
                                <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                                </Trigger>
                                <Trigger Property="Text" Value="{x:Null}">
                                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                                </Trigger>
                                <Trigger Property="IsKeyboardFocused" Value="True">
                                    <Setter Property="Background" Value="White" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>

并在您的App.xaml

<Application.Resources>
        <ResourceDictionary>
                <ResourceDictionary Source="YourStyleDictionary.xaml"/>
        </ResourceDictionary>
    </Application.Resources>

这将创建一个应用于所有TextBox的全局样式,如果您只想将它​​用于特定的TextBoxes,则为您的样式添加x:Key