0

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>     
VivekDev
  • 20,868
  • 27
  • 132
  • 202
  • 3
    Maybe you are looking for a ResourceDictionary? https://blogs.msdn.microsoft.com/wpfsldesigner/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight/ – Babbillumpa Jul 22 '16 at 08:52

1 Answers1

3

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>

and in your App.xaml

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

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

  • What if I want different text boxes to have different Content of the label. That is I should be able to set the Content in differently for each text box I apply this style to. How do I do that? – VivekDev Jul 22 '16 at 10:12
  • 1
    a look at this should help http://stackoverflow.com/questions/18431043/wpf-relativesource-in-style –  Jul 22 '16 at 10:47