2011-08-31 43 views
0

我已經做了BaseStyle,它看起來像這樣:如何在WPF中爲文本框和組合框應用相同的樣式?

<Style x:Key="BaseStyle" TargetType="{x:Type Control}"> 
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None" /> 
    <Setter Property="AllowDrop" Value="true" /> 
    <Setter Property="Background" Value="Transparent"></Setter> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
    <Setter Property="VerticalContentAlignment" Value="Stretch" /> 
    <Setter Property="FontFamily" Value="Segoe UI" /> 
    <Setter Property="FontSize" Value="12" /> 
    <Setter Property="Padding" Value="8,5,3,3" /> 
    <Setter Property="BorderThickness" Value="0" /> 
    <Setter Property="SnapsToDevicePixels" Value="True" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Control}"> 
       <Grid> 
        <Border x:Name="BorderBase" Background="White" BorderThickness="1,1,1.4,1.4" BorderBrush="Silver" CornerRadius="4" /> 
        <Label x:Name="TextPrompt" Content="{TemplateBinding Tag}" Visibility="Collapsed" Focusable="False" Foreground="Silver"></Label> 
        <ScrollViewer Margin="0" x:Name="PART_ContentHost" Foreground="{DynamicResource OutsideFontColor}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsFocused" Value="True"> 
         <Setter Property="BorderThickness" TargetName="BorderBase" Value="1,1,2.4,2.4"></Setter> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate x:Name="InspectorErrorTemplate"> 
       <StackPanel Orientation="Vertical"> 
        <Border BorderBrush="Red" BorderThickness="1" CornerRadius="4"> 
         <AdornedElementPlaceholder Name="adornerPlaceholder"/> 
        </Border> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter>   
</Style> 

而且用它來把它應用到一個文本框,其正常工作是這樣的:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseStyle}" /> 

現在,我想我可以簡單地在組合框的文本框中使用相同的樣式。所以我想我必須在這部分補充一下:

<ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}"> 
    <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" /> 
    <ControlTemplate.Triggers> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

但是,我不能添加像支持算法FMP =「{StaticResource的BaseStyle}」在ControlTemplate中做出如文本框在接收到焦點時獲取不同的邊框(請參閱BaseStyle中的IsFocused觸發器),或者在驗證被觸發的情況下顯示紅色彎曲的拐角......我做錯了什麼?

回答

0

嗨,你正在使用不同的邊框顏色爲不同的文本框,這是唯一的問題在這裏。還有其他幾個選項,但我覺得以下選項是很好的選擇。

您可以創建自己的UserControl,並在其中保留一個TextBox。您可以在UserControl中添加一個新的DependencyProperty- BorderColor屬性。因此,根據內部的BorderColor屬性值,您可以更改邊框的顏色。所以在這裏你不必擔心多個Style或任何繼承。

是不是?

0

TextBox的模板與ComboBox的模板有着根本的不同。所以你必須有不同的模板。

您可以擁有一種基本樣式來定義共享屬性(如Padding,FontFamily等),而無需定義Template屬性。然後製作兩個樣式:一個與TargetType設置爲TextBox;另一個與TargetType設置爲ComboBox。這些樣式中的每一種都將基於您的基本樣式,併爲模板(以及兩個控件之間不共享的其他屬性)提供附加定義。

相關問題