如何将带有触发器的 WPF 文本框样式放入 Windows 资源中 [英] How to put WPF textbox style with triggers into windows resources

查看:21
本文介绍了如何将带有触发器的 WPF 文本框样式放入 Windows 资源中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following WPF textbox with Style associated with it. Is there any way by which I can push the TextBox.Style into resources so it can be reused?

<TextBox HorizontalContentAlignment="Center" Text="{Binding IpAddress, Mode=TwoWay}" ToolTip="Ip Address of camera">
                        <TextBox.Style>
                            <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
                                <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>
                        </TextBox.Style>
                    </TextBox>     

推荐答案

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

Create a Resource Dictionary put your style in it and add it to 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 中

and in your App.xaml

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

这将创建一个应用于所有文本框的全局样式,如果您只想将其用于特定的文本框,请为您的样式添加一个 x:Key

This will create a global style applied on all TextBoxes if you just want to use it for specific TextBoxes add a x:Key to your Style

这篇关于如何将带有触发器的 WPF 文本框样式放入 Windows 资源中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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