combo box inside a user control disappears when style is applied in wpf

I'm trying to apply a style to a combo box but instead of getting applied the combo box itself disappears. Please check the following xaml code for user control.

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

Here PASCustomComboBox is a class which inherites from combo box.

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);
    }
}

The problem is that you are redefining the ControlTemplate without any visual tree in it:

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

You'll want the triggers on the style instead of the controltemplate:

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

You dont specify any visual elements in the control template of your style, just a trigger. It will render the empty template iirc.

Better edit the Trigger collection of the ComboBox in your style to add that trigger and you will keep the default ControlTemplate.

链接地址: http://www.djcxy.com/p/34474.html

上一篇: xmlns映射到WPF中的类在哪里?

下一篇: 当在wpf中应用样式时,用户控件内的组合框消失