Skip to content

Commit

Permalink
feat: 实现 ListBoxItem 更多属性, 优化 ScrollViewer 平滑滚动算法
Browse files Browse the repository at this point in the history
  • Loading branch information
SlimeNull committed Apr 25, 2024
1 parent 779a011 commit 1c49426
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 67 deletions.
107 changes: 107 additions & 0 deletions EleCho.WpfSuite/Controls/ListBoxItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace EleCho.WpfSuite
{
Expand All @@ -15,11 +16,117 @@ public CornerRadius CornerRadius
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
public Brush HoverForeground
{
get { return (Brush)GetValue(HoverForegroundProperty); }
set { SetValue(HoverForegroundProperty, value); }
}

public Brush HoverBackground
{
get { return (Brush)GetValue(HoverBackgroundProperty); }
set { SetValue(HoverBackgroundProperty, value); }
}

public Brush HoverBorderBrush
{
get { return (Brush)GetValue(HoverBorderBrushProperty); }
set { SetValue(HoverBorderBrushProperty, value); }
}

public Brush SelectedForeground
{
get { return (Brush)GetValue(SelectedForegroundProperty); }
set { SetValue(SelectedForegroundProperty, value); }
}

public Brush SelectedBackground
{
get { return (Brush)GetValue(SelectedBackgroundProperty); }
set { SetValue(SelectedBackgroundProperty, value); }
}

public Brush SelectedBorderBrush
{
get { return (Brush)GetValue(SelectedBorderBrushProperty); }
set { SetValue(SelectedBorderBrushProperty, value); }
}

public Brush SelectedInactiveForeground
{
get { return (Brush)GetValue(SelectedInactiveForegroundProperty); }
set { SetValue(SelectedInactiveForegroundProperty, value); }
}

public Brush SelectedInactiveBackground
{
get { return (Brush)GetValue(SelectedInactiveBackgroundProperty); }
set { SetValue(SelectedInactiveBackgroundProperty, value); }
}

public Brush SelectedInactiveBorderBrush
{
get { return (Brush)GetValue(SelectedInactiveBorderBrushProperty); }
set { SetValue(SelectedInactiveBorderBrushProperty, value); }
}

public Brush DisabledForeground
{
get { return (Brush)GetValue(DisabledForegroundProperty); }
set { SetValue(DisabledForegroundProperty, value); }
}

public Brush DisabledBackground
{
get { return (Brush)GetValue(DisabledBackgroundProperty); }
set { SetValue(DisabledBackgroundProperty, value); }
}

public Brush DisabledBorderBrush
{
get { return (Brush)GetValue(DisabledBorderBrushProperty); }
set { SetValue(DisabledBorderBrushProperty, value); }
}


public static readonly DependencyProperty CornerRadiusProperty =
Border.CornerRadiusProperty.AddOwner(typeof(ListBoxItem));


public static readonly DependencyProperty HoverForegroundProperty =
DependencyProperty.Register(nameof(HoverForeground), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty HoverBackgroundProperty =
DependencyProperty.Register(nameof(HoverBackground), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty HoverBorderBrushProperty =
DependencyProperty.Register(nameof(HoverBorderBrush), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty SelectedForegroundProperty =
DependencyProperty.Register(nameof(SelectedForeground), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty SelectedBackgroundProperty =
DependencyProperty.Register(nameof(SelectedBackground), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty SelectedBorderBrushProperty =
DependencyProperty.Register(nameof(SelectedBorderBrush), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty SelectedInactiveForegroundProperty =
DependencyProperty.Register(nameof(SelectedInactiveForeground), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty SelectedInactiveBackgroundProperty =
DependencyProperty.Register(nameof(SelectedInactiveBackground), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty SelectedInactiveBorderBrushProperty =
DependencyProperty.Register(nameof(SelectedInactiveBorderBrush), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty DisabledForegroundProperty =
DependencyProperty.Register(nameof(DisabledForeground), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty DisabledBackgroundProperty =
DependencyProperty.Register(nameof(DisabledBackground), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));

public static readonly DependencyProperty DisabledBorderBrushProperty =
DependencyProperty.Register(nameof(DisabledBorderBrush), typeof(Brush), typeof(ListBoxItem), new FrameworkPropertyMetadata(null));
}
}
121 changes: 107 additions & 14 deletions EleCho.WpfSuite/Controls/ListBoxItemResources.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="Item.MouseOver.Background" Color="#1F26A0DA"/>
<SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/>
<SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/>
<SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA"/>

<Style TargetType="{x:Type ws:ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Padding" Value="4,1"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="HoverBackground" Value="#1F26A0DA"/>
<Setter Property="HoverBorderBrush" Value="#a826A0Da"/>
<Setter Property="SelectedBackground" Value="#3D26A0DA"/>
<Setter Property="SelectedBorderBrush" Value="#FF26A0DA"/>
<Setter Property="SelectedInactiveBackground" Value="#3DDADADA"/>
<Setter Property="SelectedInactiveBorderBrush" Value="#FFDADADA"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
<Setter Property="Template">
Expand All @@ -36,34 +37,126 @@
CornerRadius="{TemplateBinding CornerRadius}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<ContentPresenter x:Name="PART_ContentPresenter"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/>
<Setter Property="Background" TargetName="Bd">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="HoverBackground"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="Background"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" TargetName="Bd">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="HoverBorderBrush"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="BorderBrush"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground" TargetName="PART_ContentPresenter">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="HoverForeground"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="Foreground"/>
</MultiBinding>
</Setter.Value>
</Setter>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="False"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/>
<Setter Property="Background" TargetName="Bd">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="SelectedInactiveBackground"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="Background"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" TargetName="Bd">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="SelectedInactiveBorderBrush"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="BorderBrush"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground" TargetName="PART_ContentPresenter">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="SelectedInactiveForeground"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="Foreground"/>
</MultiBinding>
</Setter.Value>
</Setter>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="True"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
<Setter Property="Background" TargetName="Bd">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="SelectedBackground"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="Background"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" TargetName="Bd">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="SelectedBorderBrush"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="BorderBrush"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground" TargetName="PART_ContentPresenter">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="SelectedForeground"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="Foreground"/>
</MultiBinding>
</Setter.Value>
</Setter>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
<Setter Property="Background" TargetName="Bd">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="DisabledBackground"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="Background"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" TargetName="Bd">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="DisabledBorderBrush"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="BorderBrush"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground" TargetName="PART_ContentPresenter">
<Setter.Value>
<MultiBinding Converter="{x:Static ws:FallbackConverter.Instance}">
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="DisabledForeground"/>
<Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="Foreground"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Expand Down
Loading

0 comments on commit 1c49426

Please sign in to comment.