簡體   English   中英

具有更寬滾動條的WPF列表框

[英]WPF Listbox with wider scrollbars

我是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>

這種解決方案不是我最喜歡的解決方案,因為它暗示着在包含“經典”列表框的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>

.cs文件是:

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

這種方法是正確的還是更好的方法應該包含用戶控件而不是自定義控件?

如果我對您的理解正確,那么您就有一個自定義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>

我嘗試將這種樣式放置在(a)窗口和(b)應用程序的資源集合中,並且在兩種情況下都可以正常工作,因此,我假設將其放置在generic.xaml也可以使用。

那這個呢?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM