2010-09-07 74 views
0

我試圖將樣式應用於組合框,但不是應用組合框自身消失。請檢查以下用於用戶控制的xaml代碼。用戶控件中的組合框在wpf中應用樣式時消失

 
<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna" 
    x:Class="Guardian.PAS.PASFramework.UI.WPF.PASComboBox" 
    xmlns:local="clr-namespace:Guardian.PAS.PASFramework.UI.WPF" 
    Height="26" Width="100" VerticalAlignment="Center" > 
    <UserControl.Resources> 
     <Style x:Key="comboBoxStyle" TargetType="{x:Type local:PASCustomComboBox}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type local:PASCustomComboBox}"> 
         <ControlTemplate.Triggers> 
          <Trigger Property="local:PASCustomComboBox.IsEnabled" Value="false"> 
           <Setter Property="Background" Value="Red"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </UserControl.Resources> 
    <Canvas Name="canvas" Height="23" Width="Auto" VerticalAlignment="Center"> 
     <Label Height="23" Name="lblCaption" Width="20" VerticalAlignment="Center">aaa</Label> 
     <local:PASCustomComboBox Height="23" x:Name="cmbComboBoxControl" VerticalAlignment="Center" Width="50" 
        IsEditable="True" Style="{StaticResource comboBoxStyle}"> 
      </local:PASCustomComboBox> 
     <Button Height="23" Name="btnSearch" Width="25" Click="btnSearch_Click" Visibility="Collapsed" 
       VerticalAlignment="Center">...</Button> 
     <Label Height="23" Name="lblDescription" VerticalAlignment="Center" Width="20" Foreground="Blue"> 

     </Label> 

    </Canvas> 
</UserControl> 

這裏PASCustomComboBox是一個從組合框繼承的類。

public class PASCustomComboBox : ComboBox 
{ 
    protected override void OnPreviewKeyDown(KeyEventArgs e) 
    { 
     if (e.Key == Key.Down || e.Key == Key.Up) 
     { 
      e.Handled = true; 
      return; 
     } 

     base.OnPreviewKeyDown(e); 
    } 
} 

回答

1

的問題是,你正在重新定義的ControlTemplate沒有任何視覺樹是:

<Style x:Key="comboBoxStyle" TargetType="{x:Type local:PASCustomComboBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:PASCustomComboBox}"> 
       <ControlTemplate.Triggers> 
        <Trigger Property="local:PASCustomComboBox.IsEnabled" Value="false"> 
         <Setter Property="Background" Value="Red"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

你想在風格,而不是控件模板觸發器:

<Style x:Key="comboBoxStyle" TargetType="{x:Type local:PASCustomComboBox}"> 
    <Style.Triggers> 
     <Trigger Property="local:PASCustomComboBox.IsEnabled" Value="false"> 
      <Setter Property="Background" Value="Red"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
0

你不指定任何視覺元素在你的風格的控制模板,只是一個觸發器。 它將呈現空模板iirc。

更好地編輯樣式中的ComboBox的Trigger集合以添加該觸發器,並且您將保留默認的ControlTemplate。