优化更新代码,添加界面功能并整合
This commit is contained in:
BIN
WPFluent/Controls/ThumbRate/ThumbRate.bmp
Normal file
BIN
WPFluent/Controls/ThumbRate/ThumbRate.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 824 B |
100
WPFluent/Controls/ThumbRate/ThumbRate.cs
Normal file
100
WPFluent/Controls/ThumbRate/ThumbRate.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
|
||||
|
||||
|
||||
using WPFluent.Input;
|
||||
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace WPFluent.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Allows to rate positively or negatively by clicking on one of the thumbs.
|
||||
/// </summary>
|
||||
public class ThumbRate : System.Windows.Controls.Control
|
||||
{
|
||||
/// <summary>
|
||||
/// Identifies the <see cref="StateChanged"/> routed event.
|
||||
/// </summary>
|
||||
public static readonly RoutedEvent StateChangedEvent = EventManager.RegisterRoutedEvent(
|
||||
nameof(StateChanged),
|
||||
RoutingStrategy.Bubble,
|
||||
typeof(TypedEventHandler<ThumbRate, RoutedEventArgs>),
|
||||
typeof(ThumbRate));
|
||||
/// <summary>
|
||||
/// Identifies the <see cref="State"/> dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
|
||||
nameof(State),
|
||||
typeof(ThumbRateState),
|
||||
typeof(ThumbRate),
|
||||
new PropertyMetadata(ThumbRateState.None, OnStateChanged));
|
||||
|
||||
/// <summary>
|
||||
/// Identifies the <see cref="TemplateButtonCommand"/> dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TemplateButtonCommandProperty = DependencyProperty.Register(
|
||||
nameof(TemplateButtonCommand),
|
||||
typeof(IRelayCommand),
|
||||
typeof(ThumbRate),
|
||||
new PropertyMetadata(null));
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ThumbRate"/> class and attaches <see
|
||||
/// cref="TemplateButtonCommand"/>.
|
||||
/// </summary>
|
||||
public ThumbRate()
|
||||
{ SetValue(TemplateButtonCommandProperty, new RelayCommand<ThumbRateState>(OnTemplateButtonClick)); }
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when <see cref="State"/> is changed.
|
||||
/// </summary>
|
||||
public event TypedEventHandler<ThumbRate, RoutedEventArgs> StateChanged
|
||||
{
|
||||
add => AddHandler(StateChangedEvent, value);
|
||||
remove => RemoveHandler(StateChangedEvent, value);
|
||||
}
|
||||
|
||||
private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if(d is not ThumbRate thumbRate)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
thumbRate.OnStateChanged((ThumbRateState)e.OldValue, (ThumbRateState)e.NewValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This virtual method is called when <see cref="State"/> is changed.
|
||||
/// </summary>
|
||||
protected virtual void OnStateChanged(ThumbRateState previousState, ThumbRateState currentState)
|
||||
{ RaiseEvent(new RoutedEventArgs(StateChangedEvent, this)); }
|
||||
|
||||
/// <summary>
|
||||
/// Triggered by clicking a button in the control template.
|
||||
/// </summary>
|
||||
protected virtual void OnTemplateButtonClick(ThumbRateState parameter)
|
||||
{
|
||||
if(State == parameter)
|
||||
{
|
||||
SetCurrentValue(StateProperty, ThumbRateState.None);
|
||||
return;
|
||||
}
|
||||
|
||||
SetCurrentValue(StateProperty, parameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the value determining the current state of the control.
|
||||
/// </summary>
|
||||
public ThumbRateState State
|
||||
{
|
||||
get => (ThumbRateState)GetValue(StateProperty);
|
||||
set => SetValue(StateProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the command triggered when clicking the button.
|
||||
/// </summary>
|
||||
public IRelayCommand TemplateButtonCommand => (IRelayCommand)GetValue(TemplateButtonCommandProperty);
|
||||
}
|
||||
71
WPFluent/Controls/ThumbRate/ThumbRate.xaml
Normal file
71
WPFluent/Controls/ThumbRate/ThumbRate.xaml
Normal file
@@ -0,0 +1,71 @@
|
||||
<!--
|
||||
This Source Code Form is subject to the terms of the MIT License.
|
||||
If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
|
||||
Copyright (C) Leszek Pomianowski and WPF UI Contributors.
|
||||
All Rights Reserved.
|
||||
-->
|
||||
|
||||
<ResourceDictionary
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:controls="clr-namespace:WPFluent.Controls">
|
||||
|
||||
<Style x:Key="DefaultUiThumbRateStyle" TargetType="{x:Type controls:ThumbRate}">
|
||||
<Setter Property="Background" Value="Transparent" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource ThumbRateForeground}" />
|
||||
<Setter Property="FontSize" Value="24" />
|
||||
<Setter Property="Focusable" Value="False" />
|
||||
<Setter Property="KeyboardNavigation.IsTabStop" Value="False" />
|
||||
<Setter Property="SnapsToDevicePixels" Value="True" />
|
||||
<Setter Property="OverridesDefaultStyle" Value="True" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type controls:ThumbRate}">
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<controls:Button
|
||||
Grid.Column="0"
|
||||
Background="Transparent"
|
||||
BorderBrush="Transparent"
|
||||
Command="{Binding Path=TemplateButtonCommand, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
|
||||
CommandParameter="{x:Static controls:ThumbRateState.Liked}"
|
||||
Foreground="{TemplateBinding Foreground}">
|
||||
<controls:SymbolIcon
|
||||
x:Name="ThumbsUpButtonIcon"
|
||||
FontSize="{TemplateBinding FontSize}"
|
||||
Symbol="ThumbLike24" />
|
||||
</controls:Button>
|
||||
|
||||
<controls:Button
|
||||
Grid.Column="1"
|
||||
Background="Transparent"
|
||||
BorderBrush="Transparent"
|
||||
Command="{Binding Path=TemplateButtonCommand, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
|
||||
CommandParameter="{x:Static controls:ThumbRateState.Disliked}"
|
||||
Foreground="{TemplateBinding Foreground}">
|
||||
<controls:SymbolIcon
|
||||
x:Name="ThumbsDownButtonIcon"
|
||||
FontSize="{TemplateBinding FontSize}"
|
||||
Symbol="ThumbDislike24" />
|
||||
</controls:Button>
|
||||
</Grid>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="State" Value="Liked">
|
||||
<Setter TargetName="ThumbsUpButtonIcon" Property="Filled" Value="True" />
|
||||
</Trigger>
|
||||
<Trigger Property="State" Value="Disliked">
|
||||
<Setter TargetName="ThumbsDownButtonIcon" Property="Filled" Value="True" />
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<Style BasedOn="{StaticResource DefaultUiThumbRateStyle}" TargetType="{x:Type controls:ThumbRate}" />
|
||||
|
||||
</ResourceDictionary>
|
||||
26
WPFluent/Controls/ThumbRate/ThumbRateState.cs
Normal file
26
WPFluent/Controls/ThumbRate/ThumbRateState.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace WPFluent.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// States of the <see cref="ThumbRate"/> control.
|
||||
/// </summary>
|
||||
public enum ThumbRateState
|
||||
{
|
||||
/// <summary>
|
||||
/// No thumb has been clicked.
|
||||
/// </summary>
|
||||
None,
|
||||
|
||||
/// <summary>
|
||||
/// The thumb up has been clicked.
|
||||
/// </summary>
|
||||
Liked,
|
||||
|
||||
/// <summary>
|
||||
/// The thumb down has been clicked.
|
||||
/// </summary>
|
||||
Disliked,
|
||||
}
|
||||
Reference in New Issue
Block a user