insilenzio
insilenzio

Reputation: 938

WPF Listbox with wider scrollbars

I'm totally new on WPF and I need your help for creating a wpf custom ListBox with scrollbar wider than the default.

I've found a solution that works fine for a Window WPF including a ListBox:

<Window x:Class="iFixCustomControlsTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cc="clr-namespace:iFixCustomControls;assembly=iFixCustomControls"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox HorizontalAlignment="Left" Height="92" Margin="56,88,0,0" VerticalAlignment="Top" Width="357" ScrollViewer.VerticalScrollBarVisibility="Visible"/>
    </Grid>

    <Window.Resources>
        <Style TargetType="ScrollBar">
            <Setter Property="Width" Value="100"/>
        </Style>        
    </Window.Resources>
</Window>

This solution is not the my favorite one, because it implies to write code in a Window including a "classic" Listbox. What I need is a way to modify scrollbar inside the Listbox (if I understood fine) in Generic.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:iFixCustomControls">
    <Style TargetType="local:iFixCustomListBox" BasedOn="{StaticResource {x:Type ListBox}}">
        <!--  
              Setting scrollbar wider than default
              Something like:
              <Style TargetType="ScrollBar">
                  <Setter Property="Width" Value="100"/>
              </Style>
        -->
    </Style>
</ResourceDictionary>

.cs file is:

public class iFixCustomListBox : ListBox
{
    static iFixCustomListBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(iFixCustomListBox), new FrameworkPropertyMetadata(typeof(iFixCustomListBox)));
    }
}

Is this approach correct or a better way should involve User Control instead Custom Controls?

Upvotes: 0

Views: 4786

Answers (2)

Steven Rands
Steven Rands

Reputation: 5421

If I understand you correctly you have a custom control type derived from ListBox and you want every instance of that control to have a wider vertical scrollbar.

If so, you can just use a custom style for your control (which you probably have already), and add a ScrollBar style to that style's Resources collection:

<Style TargetType="{x:Type local:iFixCustomListBox}">
    <Style.Resources>
        <Style TargetType="{x:Type ScrollBar}">
            <Setter Property="Width" Value="100" />
        </Style>
    </Style.Resources>
</Style>

I tried with this style placed in the resources collection of (a) a window, and (b) the application, and it worked fine in both cases, so I assume it would also work if placed in generic.xaml.

Upvotes: 6

Yuri Dorokhov
Yuri Dorokhov

Reputation: 726

What about this?

<ScrollViewer Width="100">
      <ListBox ...>
</ScrollViewer>

Upvotes: 0

Related Questions