2016-07-22 23 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

相關問題