具有更宽滚动条的 WPF 列表框
WPF Listbox with wider scrollbars
我是 WPF 的新手,我需要您的帮助来创建滚动条比默认滚动条更宽的 wpf 自定义列表框。
我找到了一个适用于包含 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>
这个解决方案不是我最喜欢的解决方案,因为它意味着要在 Window 中编写代码,其中包括一个 "classic" 列表框。我需要的是一种在 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 文件是:
public class iFixCustomListBox : ListBox
{
static iFixCustomListBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(iFixCustomListBox), new FrameworkPropertyMetadata(typeof(iFixCustomListBox)));
}
}
这种方法是否正确,或者更好的方法应该涉及用户控件而不是自定义控件?
这个呢?
<ScrollViewer Width="100">
<ListBox ...>
</ScrollViewer>
如果我对你的理解正确的话,你有一个派生自 ListBox
的自定义控件类型,并且你希望该控件的每个实例都有一个更宽的垂直滚动条。
如果是这样,您可以只为控件使用自定义样式(您可能已经有了),然后将 ScrollBar
样式添加到 that 样式的 Resources
collection:
<Style TargetType="{x:Type local:iFixCustomListBox}">
<Style.Resources>
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Width" Value="100" />
</Style>
</Style.Resources>
</Style>
我尝试将此样式放置在 (a) a window 和 (b) 应用程序的资源 collection 中,并且在这两种情况下都运行良好,所以我认为它会如果放在 generic.xaml
.
中也可以工作
我是 WPF 的新手,我需要您的帮助来创建滚动条比默认滚动条更宽的 wpf 自定义列表框。
我找到了一个适用于包含 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>
这个解决方案不是我最喜欢的解决方案,因为它意味着要在 Window 中编写代码,其中包括一个 "classic" 列表框。我需要的是一种在 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 文件是:
public class iFixCustomListBox : ListBox
{
static iFixCustomListBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(iFixCustomListBox), new FrameworkPropertyMetadata(typeof(iFixCustomListBox)));
}
}
这种方法是否正确,或者更好的方法应该涉及用户控件而不是自定义控件?
这个呢?
<ScrollViewer Width="100">
<ListBox ...>
</ScrollViewer>
如果我对你的理解正确的话,你有一个派生自 ListBox
的自定义控件类型,并且你希望该控件的每个实例都有一个更宽的垂直滚动条。
如果是这样,您可以只为控件使用自定义样式(您可能已经有了),然后将 ScrollBar
样式添加到 that 样式的 Resources
collection:
<Style TargetType="{x:Type local:iFixCustomListBox}">
<Style.Resources>
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Width" Value="100" />
</Style>
</Style.Resources>
</Style>
我尝试将此样式放置在 (a) a window 和 (b) 应用程序的资源 collection 中,并且在这两种情况下都运行良好,所以我认为它会如果放在 generic.xaml
.