具有更宽滚动条的WPF列表框

ins*_*zio 0 c# wpf custom-controls

我是WPF的新手,我需要您的帮助来创建一个wpf自定义ListBox,其滚动条比默认宽度宽。

我找到了一种适用于包括ListBox的Window WPF的解决方案:

<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>
Run Code Online (Sandbox Code Playgroud)

这种解决方案不是我最喜欢的解决方案,因为它暗示着在包含“经典”列表框的Window中编写代码。我需要的是一种在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>
Run Code Online (Sandbox Code Playgroud)

.cs文件是:

public class iFixCustomListBox : ListBox
{
    static iFixCustomListBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(iFixCustomListBox), new FrameworkPropertyMetadata(typeof(iFixCustomListBox)));
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方法是正确的还是更好的方法应该包含用户控件而不是自定义控件?

Ste*_*nds 5

如果我对您的理解正确,那么您就有一个自定义控件类型的派生对象,ListBox并且您希望该控件的每个实例都具有一个较宽的垂直滚动条。

如果是这样,您可以为控件使用自定义样式(您可能已经拥有),然后将ScrollBar样式添加到样式的Resources集合中:

<Style TargetType="{x:Type local:iFixCustomListBox}">
    <Style.Resources>
        <Style TargetType="{x:Type ScrollBar}">
            <Setter Property="Width" Value="100" />
        </Style>
    </Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)

我尝试将这种样式放置在(a)窗口和(b)应用程序的资源集合中,并且在两种情况下都可以正常工作,因此我假设将其放置在中也可以使用generic.xaml