2016-07-22 62 views
1

我有以下与其关联的样式的WPF文本框。有什么办法可以将TextBox.Style变成资源,以便它可以重复使用?如何将带触发器的WPF文本框样式放入Windows资源中

<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>  
+3

也许您在寻找ResourceDictionary? https://blogs.msdn.microsoft.com/wpfsldesigner/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight/ – CiccioRocca

回答

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> 

这将创建如果您只是想将其用于特定文本框,则应用于所有文本框的全局样式添加ax:键入您的样式

+0

如果我想让不同的文本框具有不同的内容的标签。这是我应该能够在 对于每个文本框我应用此样式不同。我怎么做? – VivekDev

+0

看看这应该有助于http://stackoverflow.com/questions/18431043/wpf-relativesource-in-style – Johannes