优化更新代码,添加界面功能并整合
This commit is contained in:
103
WPFluent/Controls/DropDownButton/DropDownButton.cs
Normal file
103
WPFluent/Controls/DropDownButton/DropDownButton.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
|
||||
|
||||
|
||||
using System.Windows.Controls;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace WPFluent.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// A control that drop downs a flyout of choices from which one can be chosen.
|
||||
/// </summary>
|
||||
public class DropDownButton : Button
|
||||
{
|
||||
/// <summary>
|
||||
/// Identifies the <see cref="Flyout"/> dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty FlyoutProperty = DependencyProperty.Register(
|
||||
nameof(Flyout),
|
||||
typeof(object),
|
||||
typeof(DropDownButton),
|
||||
new PropertyMetadata(null, OnFlyoutChanged));
|
||||
|
||||
/// <summary>
|
||||
/// Identifies the <see cref="IsDropDownOpen"/> dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsDropDownOpenProperty = DependencyProperty.Register(
|
||||
nameof(IsDropDownOpen),
|
||||
typeof(bool),
|
||||
typeof(DropDownButton),
|
||||
new PropertyMetadata(false));
|
||||
private ContextMenu? _contextMenu;
|
||||
|
||||
private static void OnFlyoutChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if(d is DropDownButton dropDownButton)
|
||||
{
|
||||
dropDownButton.OnFlyoutChanged(e.NewValue);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnClick()
|
||||
{
|
||||
base.OnClick();
|
||||
|
||||
if(_contextMenu is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_contextMenu.SetCurrentValue(MinWidthProperty, ActualWidth);
|
||||
_contextMenu.SetCurrentValue(ContextMenu.PlacementTargetProperty, this);
|
||||
_contextMenu.SetCurrentValue(
|
||||
ContextMenu.PlacementProperty,
|
||||
System.Windows.Controls.Primitives.PlacementMode.Bottom);
|
||||
_contextMenu.SetCurrentValue(ContextMenu.IsOpenProperty, true);
|
||||
}
|
||||
|
||||
protected virtual void OnContextMenuClosed(object sender, RoutedEventArgs e)
|
||||
{ SetCurrentValue(IsDropDownOpenProperty, false); }
|
||||
|
||||
protected virtual void OnContextMenuOpened(object sender, RoutedEventArgs e)
|
||||
{ SetCurrentValue(IsDropDownOpenProperty, true); }
|
||||
|
||||
/// <summary>
|
||||
/// This method is invoked when the <see cref="FlyoutProperty"/> changes.
|
||||
/// </summary>
|
||||
/// <param name="value">The new value of <see cref="FlyoutProperty"/>.</param>
|
||||
protected virtual void OnFlyoutChanged(object value)
|
||||
{
|
||||
if(value is ContextMenu contextMenu)
|
||||
{
|
||||
_contextMenu = contextMenu;
|
||||
_contextMenu.Opened += OnContextMenuOpened;
|
||||
_contextMenu.Closed += OnContextMenuClosed;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnIsDropDownOpenChanged(bool currentValue)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the flyout associated with this button.
|
||||
/// </summary>
|
||||
[Bindable(true)]
|
||||
public object Flyout { get => GetValue(FlyoutProperty); set => SetValue(FlyoutProperty, value); }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the drop-down for a button is currently open.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// <see langword="true"/> if the drop-down is open; otherwise, <see langword="false"/>. The default is <see
|
||||
/// langword="false"/>.
|
||||
/// </returns>
|
||||
[Bindable(true)]
|
||||
[Browsable(false)]
|
||||
[Category("Appearance")]
|
||||
public bool IsDropDownOpen
|
||||
{
|
||||
get => (bool)GetValue(IsDropDownOpenProperty);
|
||||
set => SetValue(IsDropDownOpenProperty, value);
|
||||
}
|
||||
}
|
||||
120
WPFluent/Controls/DropDownButton/DropDownButton.xaml
Normal file
120
WPFluent/Controls/DropDownButton/DropDownButton.xaml
Normal file
@@ -0,0 +1,120 @@
|
||||
<!--
|
||||
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.
|
||||
|
||||
Based on Microsoft XAML for Win UI
|
||||
Copyright (c) Microsoft Corporation. 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"
|
||||
xmlns:markup="clr-namespace:WPFluent.Markup">
|
||||
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="pack://application:,,,/WPFluent;component/Controls/Button/Button.xaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
|
||||
<Thickness x:Key="ButtonChevronIconMargin">8,0,0,0</Thickness>
|
||||
|
||||
<Style
|
||||
x:Key="DefaultUiDropDownButtonStyle"
|
||||
BasedOn="{StaticResource DefaultUiButtonStyle}"
|
||||
TargetType="{x:Type controls:DropDownButton}">
|
||||
<Setter Property="IsDropDownOpen" Value="False" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type controls:DropDownButton}">
|
||||
<Border
|
||||
x:Name="ContentBorder"
|
||||
Grid.Row="0"
|
||||
Width="{TemplateBinding Width}"
|
||||
Height="{TemplateBinding Height}"
|
||||
MinWidth="{TemplateBinding MinWidth}"
|
||||
MinHeight="{TemplateBinding MinHeight}"
|
||||
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalAlignment}"
|
||||
Background="{TemplateBinding Background}"
|
||||
CornerRadius="{TemplateBinding CornerRadius}">
|
||||
<Border
|
||||
Padding="{TemplateBinding Padding}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
CornerRadius="{TemplateBinding CornerRadius}">
|
||||
<Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<ContentPresenter
|
||||
x:Name="ControlIcon"
|
||||
Grid.Column="0"
|
||||
Margin="{StaticResource ButtonIconMargin}"
|
||||
VerticalAlignment="Center"
|
||||
Content="{TemplateBinding Icon}"
|
||||
ContentTemplate="{TemplateBinding ContentTemplate}"
|
||||
Focusable="False"
|
||||
TextElement.FontSize="{TemplateBinding FontSize}"
|
||||
TextElement.Foreground="{TemplateBinding Foreground}" />
|
||||
|
||||
<ContentPresenter
|
||||
x:Name="ContentPresenter"
|
||||
Grid.Column="1"
|
||||
VerticalAlignment="Center"
|
||||
Content="{TemplateBinding Content}"
|
||||
TextElement.Foreground="{TemplateBinding Foreground}" />
|
||||
|
||||
<Grid Grid.Column="2" Margin="{StaticResource ButtonChevronIconMargin}">
|
||||
<controls:SymbolIcon FontSize="10" Symbol="ChevronDown24" />
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Border>
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<MultiTrigger>
|
||||
<MultiTrigger.Conditions>
|
||||
<Condition Property="IsMouseOver" Value="True" />
|
||||
<Condition Property="IsPressed" Value="False" />
|
||||
</MultiTrigger.Conditions>
|
||||
<Setter TargetName="ContentBorder" Property="Background" Value="{Binding MouseOverBackground, RelativeSource={RelativeSource TemplatedParent}}" />
|
||||
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{Binding MouseOverBorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
|
||||
</MultiTrigger>
|
||||
<MultiTrigger>
|
||||
<MultiTrigger.Conditions>
|
||||
<Condition Property="IsMouseOver" Value="True" />
|
||||
<Condition Property="IsPressed" Value="True" />
|
||||
</MultiTrigger.Conditions>
|
||||
<Setter TargetName="ContentBorder" Property="Background" Value="{Binding PressedBackground, RelativeSource={RelativeSource TemplatedParent}}" />
|
||||
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{Binding PressedBorderBrush, RelativeSource={RelativeSource TemplatedParent}}" />
|
||||
<Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{Binding PressedForeground, RelativeSource={RelativeSource TemplatedParent}}" />
|
||||
<Setter TargetName="ControlIcon" Property="TextElement.Foreground" Value="{Binding PressedForeground, RelativeSource={RelativeSource TemplatedParent}}" />
|
||||
</MultiTrigger>
|
||||
<Trigger Property="IsEnabled" Value="False">
|
||||
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource ControlFillColorDisabledBrush}" />
|
||||
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource ControlStrokeColorDefaultBrush}" />
|
||||
<Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{DynamicResource TextFillColorDisabledBrush}" />
|
||||
</Trigger>
|
||||
<Trigger Property="Content" Value="{x:Null}">
|
||||
<Setter TargetName="ControlIcon" Property="Margin" Value="0" />
|
||||
</Trigger>
|
||||
<Trigger Property="Content" Value="">
|
||||
<Setter TargetName="ControlIcon" Property="Margin" Value="0" />
|
||||
</Trigger>
|
||||
<Trigger Property="Icon" Value="{x:Null}">
|
||||
<Setter TargetName="ControlIcon" Property="Margin" Value="0" />
|
||||
<Setter TargetName="ControlIcon" Property="Visibility" Value="Collapsed" />
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<Style BasedOn="{StaticResource DefaultUiDropDownButtonStyle}" TargetType="{x:Type controls:DropDownButton}" />
|
||||
|
||||
</ResourceDictionary>
|
||||
Reference in New Issue
Block a user