wpf 滾動條聯動和ListBox滾動條樣式重寫
public void ScrollViewerMouseWheel(FrameworkElement fElement)
{
fElement.PreviewMouseWheel += (sender, e) =>
{
var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
eventArg.RoutedEvent = UIElement.MouseWheelEvent;
eventArg.Source = sender;
fElement.RaiseEvent(eventArg);
};
}
//判斷滾動與子控件滾動事件沖突
//private void CreateCDGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
//{
// //e.Handled = true; // 阻止事件冒泡
// if (scrollViewer.VerticalOffset == 0 && e.Delta >= 0)
// return;
// if (scrollViewer.ViewportHeight + scrollViewer.VerticalOffset >= scrollViewer.ExtentHeight && e.Delta <= 0)
// return;
// base.OnMouseWheel(e);
//}
ListBox滾動條樣式重寫
<Style x:Key="{x:Type ListBoxItem}" TargetType="ListBoxItem">
<Setter Property="Width" Value="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=119.5, UpdateSourceTrigger=PropertyChanged}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="{DynamicResource CdNoticeTimeFontBrush}" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource DefaultFocusStyle_CR6}" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Margin" Value="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=0 4.5 -0.5 4.5, UpdateSourceTrigger=PropertyChanged}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<Border
x:Name="bor1"
Width="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=119.5, UpdateSourceTrigger=PropertyChanged}"
Height="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=42, UpdateSourceTrigger=PropertyChanged}"
Background="{TemplateBinding Background}"
CornerRadius="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=9 9 9 9, UpdateSourceTrigger=PropertyChanged}">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock
FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}"
Style="{DynamicResource Subbtitle3FontStyle}"
Text="{Binding Data}"
TextAlignment="Center" />
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="{DynamicResource CdNoticeTimeHoverFontBrush}" />
<Setter Property="Background" Value="{DynamicResource CdNoticeTimeHoverBrush}" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="{DynamicResource CdNoticeTimeSelectFontBrush}" />
<Setter Property="Background" Value="{DynamicResource CdNoticeTimeSelectBrush}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Foreground" Value="{DynamicResource CdNoticeTimeSelectFontBrush}" />
<Setter Property="Background" Value="{DynamicResource CdNoticeTimeSelectBrush}" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxScrollViewerStyle" TargetType="{x:Type ScrollViewer}">
<Setter Property="PanningMode" Value="VerticalOnly" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid x:Name="Grid" Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=21, UpdateSourceTrigger=PropertyChanged}" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid
Grid.Row="0"
Grid.Column="0"
Margin="{TemplateBinding Padding}">
<ScrollContentPresenter CanContentScroll="{TemplateBinding CanContentScroll}" />
</Grid>
<ScrollBar
x:Name="PART_VerticalScrollBar"
Grid.Row="0"
Grid.Column="1"
Width="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=21, UpdateSourceTrigger=PropertyChanged}"
Maximum="{TemplateBinding ScrollableHeight}"
Orientation="Vertical"
ViewportSize="{TemplateBinding ViewportHeight}"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
Value="{TemplateBinding VerticalOffset}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</Style.Triggers>
</Style>
<!-- ScrollViewer 滾動條 -->
<Style x:Key="ScrollBarThumb" TargetType="{x:Type Thumb}">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="IsTabStop" Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Grid>
<Border
x:Name="bor"
Width="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=9, UpdateSourceTrigger=PropertyChanged}"
Background="{DynamicResource CtScrollBarThumbBrush}"
CornerRadius="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=6 6 6 6, UpdateSourceTrigger=PropertyChanged}" />
<Border
x:Name="bor1"
Width="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=9, UpdateSourceTrigger=PropertyChanged}"
Background="{DynamicResource CtScrollBarThumbOpacityBrush}"
CornerRadius="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=6 6 6 6, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<!--<Setter TargetName="bor" Property="Background">
<Setter.Value>
<SolidColorBrush Color="Blue" />
</Setter.Value>
</Setter>-->
<Setter TargetName="bor1" Property="Visibility" Value="Collapsed" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="VerticalScrollBarPageButton" TargetType="{x:Type RepeatButton}">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Focusable" Value="false" />
<Setter Property="IsTabStop" Value="false" />
<Setter Property="Opacity" Value="0.2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RepeatButton}">
<Rectangle
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Fill="{TemplateBinding Background}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- 滾動條上下按鈕 -->
<Style x:Key="VerticalScrollBarPageButton2" TargetType="{x:Type RepeatButton}">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Focusable" Value="false" />
<Setter Property="IsTabStop" Value="false" />
<Setter Property="Opacity" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RepeatButton}">
<Rectangle
Width="0"
Height="0"
Fill="#90000000" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="MyScrollbarSytle" TargetType="{x:Type ScrollBar}">
<Setter Property="Stylus.IsPressAndHoldEnabled" Value="false" />
<Setter Property="Stylus.IsFlicksEnabled" Value="false" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid x:Name="Bg" SnapsToDevicePixels="true">
<Grid.RowDefinitions>
<RowDefinition Height="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=12, UpdateSourceTrigger=PropertyChanged}" />
<RowDefinition Height="*" />
<RowDefinition Height="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=12 , UpdateSourceTrigger=PropertyChanged}" />
</Grid.RowDefinitions>
<RepeatButton
Grid.Row="0"
Command="{x:Static ScrollBar.PageUpCommand}"
Style="{StaticResource VerticalScrollBarPageButton2}" />
<Track
x:Name="PART_Track"
Grid.Row="1"
IsDirectionReversed="true"
IsEnabled="{TemplateBinding IsMouseOver}"
Maximum="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=90, UpdateSourceTrigger=PropertyChanged}"
ViewportSize="{Binding Converter={StaticResource DPIConverter}, ConverterParameter=144, UpdateSourceTrigger=PropertyChanged}">
<Track.DecreaseRepeatButton>
<RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="{StaticResource VerticalScrollBarPageButton}" />
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="{StaticResource VerticalScrollBarPageButton}" />
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb Style="{StaticResource ScrollBarThumb}" />
</Track.Thumb>
</Track>
<RepeatButton
Grid.Row="2"
Command="{x:Static ScrollBar.PageDownCommand}"
Style="{StaticResource VerticalScrollBarPageButton2}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style BasedOn="{StaticResource MyScrollbarSytle}" TargetType="ScrollBar" />

浙公網安備 33010602011771號