Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Combo box inside a user control disappears when style is applied

Combo box inside a user control disappears when style is applied

Scheduled Pinned Locked Moved WPF
wpfcsharpdatabasedotnetcom
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dashingsidds
    wrote on last edited by
    #1

    Hi, I am 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)
            {
    
    I 1 Reply Last reply
    0
    • D dashingsidds

      Hi, I am 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)
              {
      
      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      When you set the Template property for a control, you're replacing the control with whatever is inside the template... In this case, you're replacing it with nothing, since the only thing you put inside your custom template is a trigger.

      Proud to have finally moved to the A-Ark. Which one are you in?
      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

      D 1 Reply Last reply
      0
      • I Ian Shlasko

        When you set the Template property for a control, you're replacing the control with whatever is inside the template... In this case, you're replacing it with nothing, since the only thing you put inside your custom template is a trigger.

        Proud to have finally moved to the A-Ark. Which one are you in?
        Author of the Guardians Saga (Sci-Fi/Fantasy novels)

        D Offline
        D Offline
        dashingsidds
        wrote on last edited by
        #3

        Oh... Then what should i write in the control template to show the current combo box as it is??? I am a little weak in xaml..!! :)

        I 1 Reply Last reply
        0
        • D dashingsidds

          Oh... Then what should i write in the control template to show the current combo box as it is??? I am a little weak in xaml..!! :)

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4

          Don't use the Template unless you plan on replacing the entire control.

          Proud to have finally moved to the A-Ark. Which one are you in?
          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups