customize the Border property with CornerRadius for ListBox

12,775

If you want the Border within the ListBoxItems to have another CornerRadius value, you can either re-template ListBoxItem where the Border is defined, or set it implicitly in the ItemContainerStyle Resources

<ListBox ...>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <Style TargetType="Border">
                    <Setter Property="CornerRadius" Value="5"/>
                </Style>
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
    <!--...-->
</ListBox>

Edit: If you want to set CornerRadius for the ListBox, you can do the same but in Resources instead

    <ListBox ...>
        <ListBox.Resources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="10"/>
            </Style>
        </ListBox.Resources>
    <!--...-->
</ListBox>
Share:
12,775

Related videos on Youtube

soma sekhar
Author by

soma sekhar

Updated on June 04, 2022

Comments

  • soma sekhar
    soma sekhar about 2 years

    I want to customize the following Listbox-display property of border with CornerRadius=5..can anyone help me to achieve it without changing the existing datatemplate code in the following Xaml code:

    <ListBox x:Uid="lst_value"  Name="lstValues" Background="Wheat" BorderBrush="Black"
             HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="1" Height="100" Width="150"
             ItemsSource="{Binding listval}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical" Background="{Binding}">
                    <TextBlock x:Name="txtblk" Foreground="Black" FontSize="10"  TextAlignment="Left" 
                                                   FontWeight="Black" Text="{Binding}" Background="{Binding}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    
  • soma sekhar
    soma sekhar almost 13 years
    yes, we can have a border inside the listbox for every listItem...where as I need the border property with cornerradius to be applied for the entire Listbox itself....hope am clear with my question..
  • Fredrik Hedblad
    Fredrik Hedblad almost 13 years
    So you want to round the corners of the ListBox and have a Border, is that correct?
  • Fredrik Hedblad
    Fredrik Hedblad almost 13 years
    Updated my answer with how you can do this for the ListBox instead of the ListBoxItems
  • soma sekhar
    soma sekhar almost 13 years
    That was perfect... but then the items in the listbox once selected were no longer having the background color(it is just white background for the selected items) and other than that I doesn't have the default listbox behaviour(i.e., single selection and multiple selection for the items). Instead, they were highlighted or selected irrespectively....Am a completely new for WPF...it would be more helpful for me...if you could guide me the way out to resolve it..Thank you..