Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys
Could anyone please help me on this?
I need the items in my combobox to be rounded corner.
I mean each of them.
Because my each items has different color.
Thanks.

What I have tried:

XML
<ComboBoxItem x:Name="Item25" OverridesDefaultStyle="True" Foreground="Red" Background="Green" Content="Item No. 25" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" ScrollViewer.VerticalScrollBarVisibility="Disabled">
    <Border CornerRadius="8"/>
</ComboBoxItem>
Posted

Did you try any research?

E.g. KAILASH'S BLOGS: Round Corner WPF Combobox[^]
 
Share this answer
 
Comments
Dave Kreskowiak 12-Dec-23 12:12pm    
Sadly, I've come to realize that hardly anybody knows how to do research these days. Apparently, it's much too difficult compared to just bombing a forum with "GIMME THE CODEZ!"
Andre Oosthuizen 12-Dec-23 12:15pm    
True that...
Maciej Los 12-Dec-23 16:26pm    
5ed!
Sh.H. 13-Dec-23 2:27am    
Thank Chill60
Actually the blog is about rounding THE COMBOBOX, not THE ITEMS.
What I want is items individually to be rounded.
I wish I could attach here a picture of what I wanted. :-(
CHill60 13-Dec-23 5:41am    
A combo-box starts in a collapsed state and expands to show the list of items - are you sure you mean combobox and not ListBox?
What you need to do is override your style. As I can't really visualise what you're trying to achieve with this, I'll give you an example where only the selected item has a rounded border, and the background is set to green. You should really have a play around with something like this:
XML
<Window x:Class="RoundedComboBoxItem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:RoundedComboBoxItem"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
  xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2">

  <Window.Resources>
    <Style 
       TargetType="{x:Type ComboBoxItem}">
      <Setter Property="SnapsToDevicePixels"
          Value="true" />
      <Setter Property="OverridesDefaultStyle"
          Value="true" />
      <Setter Property="Foreground" Value="Red" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ComboBoxItem}">
            <Border x:Name="Border"
                Padding="2"
                SnapsToDevicePixels="true"
                Background="Transparent"
                    CornerRadius="8"
                >
              <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="SelectionStates">
                  <VisualState x:Name="Unselected" />
                  <VisualState x:Name="Selected">
                    <Storyboard>
                      <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                Storyboard.TargetProperty="(Panel.Background).
                    (SolidColorBrush.Color)">
                        <EasingColorKeyFrame KeyTime="0"
                                         Value="Green" />
                      </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                  </VisualState>
                </VisualStateGroup>
              </VisualStateManager.VisualStateGroups>
              <ContentPresenter />
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>

  <Grid>
    <ComboBox HorizontalAlignment="Left" Margin="154,133,0,0" VerticalAlignment="Top" Width="372">
      <ComboBoxItem Content="Item No. 23" />
      <ComboBoxItem Content="Item No. 24"/>
      <ComboBoxItem Content="Item No. 25">
      </ComboBoxItem>
    </ComboBox>

  </Grid>
</Window>
 
Share this answer
 
Comments
CHill60 13-Dec-23 10:18am    
5'd
Pete O'Hanlon 13-Dec-23 10:27am    
Thank you.
Sh.H. 17-Dec-23 14:05pm    
Ok good but there is no selection.
I mean if I use arrows in keyboard to navigate in the popup items, I could not find out which item I am on!
Sh.H. 17-Dec-23 14:09pm    
Also the background is not transparent and it is white.
Sh.H. 26-Dec-23 6:07am    
How may I make items center? All items shows in left of popup.

What I've tried:
<setter property="HorizontalContentAlignment" value="Center">

I tried this but doesn't work. I mean the items are at left yet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900