添加项目文件。
This commit is contained in:
344
Wpf.Ui.Extend/Controls/ComboBoxEx/ComboBoxEx.xaml
Normal file
344
Wpf.Ui.Extend/Controls/ComboBoxEx/ComboBoxEx.xaml
Normal file
@@ -0,0 +1,344 @@
|
||||
<ResourceDictionary
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:controls="http://schemas.lepo.co/wpfui/2022/xaml"
|
||||
xmlns:converters="clr-namespace:Wpf.Ui.Extend.Converters"
|
||||
xmlns:exc="clr-namespace:Wpf.Ui.Extend.Controls"
|
||||
xmlns:system="clr-namespace:System;assembly=System.Runtime">
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<!--<ResourceDictionary Source="pack://application:,,,/Wpf.Ui;component/Resources/Variables.xaml" />-->
|
||||
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui;component/Controls/ComboBox/ComboBox.xaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
<converters:BooleanToVisConverter x:Key="BooleanToVisConverter" />
|
||||
<Style x:Key="DefaultComboBoxExStyle" TargetType="{x:Type exc:ComboBoxEx}">
|
||||
<Style.Triggers>
|
||||
<Trigger Property="HeaderPlacement" Value="Left">
|
||||
<Setter Property="HorizontalAlignment" Value="Right" />
|
||||
</Trigger>
|
||||
<Trigger Property="HeaderPlacement" Value="Right">
|
||||
<Setter Property="HorizontalAlignment" Value="Left" />
|
||||
</Trigger>
|
||||
<!--<Trigger Property="MultiSelect" Value="True">
|
||||
<Setter Property="ItemContainerStyle" Value="{StaticResource DefaultMultiComboxBoxItemStyle}" />
|
||||
<Setter Property="Template" Value="{StaticResource DefaultMultiComboBoxExControlTemplate}" />
|
||||
</Trigger>-->
|
||||
</Style.Triggers>
|
||||
<!-- Universal WPF UI focus -->
|
||||
<Setter Property="FocusVisualStyle" Value="{DynamicResource DefaultControlFocusVisualStyle}" />
|
||||
<!-- Universal WPF UI focus -->
|
||||
<!-- Universal WPF UI ContextMenu -->
|
||||
<Setter Property="ContextMenu" Value="{DynamicResource DefaultControlContextMenu}" />
|
||||
<!-- Universal WPF UI ContextMenu -->
|
||||
<Setter Property="Background" Value="{DynamicResource ComboBoxBackground}" />
|
||||
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource ControlElevationBorderBrush}" />
|
||||
<Setter Property="BorderThickness" Value="{StaticResource ComboBoxBorderThemeThickness}" />
|
||||
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource ComboBoxForeground}" />
|
||||
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
||||
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
|
||||
<Setter Property="MinHeight" Value="{DynamicResource TextControlThemeMinHeight}" />
|
||||
<Setter Property="MinWidth" Value="{DynamicResource TextControlThemeMinWidth}" />
|
||||
<Setter Property="OverridesDefaultStyle" Value="True" />
|
||||
<Setter Property="Padding" Value="{DynamicResource ComboBoxPadding}" />
|
||||
<Setter Property="Popup.Placement" Value="Bottom" />
|
||||
<Setter Property="Popup.PopupAnimation" Value="Slide" />
|
||||
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
|
||||
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
|
||||
<Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
|
||||
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
|
||||
<Setter Property="VerticalAlignment" Value="Center" />
|
||||
<!-- WPF 不喜欢居中,动画很难看,鼠标按钮有时会立即点击。 -->
|
||||
<Setter Property="SnapsToDevicePixels" Value="True" />
|
||||
<Setter Property="VerticalContentAlignment" Value="Top" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type exc:ComboBoxEx}">
|
||||
<DockPanel>
|
||||
<!-- 标题 -->
|
||||
<TextBlock
|
||||
x:Name="Header_TextBlock"
|
||||
Margin="5"
|
||||
VerticalAlignment="Center"
|
||||
DockPanel.Dock="{TemplateBinding HeaderPlacement}"
|
||||
FontSize="{TemplateBinding FontSize}"
|
||||
Text="{TemplateBinding Header}" />
|
||||
<Grid
|
||||
x:Name="Grid"
|
||||
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalAlignment}">
|
||||
<!-- 强调边缘 只有IsEditable时出现 -->
|
||||
<Border
|
||||
x:Name="AccentBorder"
|
||||
BorderBrush="{DynamicResource ComboBoxBorderBrushFocused}"
|
||||
BorderThickness="{StaticResource ComboBoxAccentBorderThemeThickness}"
|
||||
CornerRadius="{TemplateBinding Border.CornerRadius}"
|
||||
Visibility="Collapsed" />
|
||||
<Border
|
||||
x:Name="ContentBorder"
|
||||
Grid.Row="0"
|
||||
MinWidth="{TemplateBinding MinWidth}"
|
||||
MinHeight="{TemplateBinding MinHeight}"
|
||||
Padding="0"
|
||||
Background="{TemplateBinding Background}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
CornerRadius="{TemplateBinding Border.CornerRadius}">
|
||||
<Grid>
|
||||
<!--
|
||||
Chevron 位于 Presenter 上,ToggleButton 位于 Chevron 上,TextBox 位于 ToggleButton 上。
|
||||
但是,TextBox 不在 Chevron 上,所以 ToggleButton 仍能工作。
|
||||
后声明在上层
|
||||
-->
|
||||
<Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid Grid.Column="0" Margin="{TemplateBinding Padding}">
|
||||
<!--#region 扩展水印-->
|
||||
<TextBlock
|
||||
x:Name="PlaceholderTextBlock"
|
||||
Padding="1,0"
|
||||
VerticalAlignment="Center"
|
||||
Focusable="False"
|
||||
FontSize="{TemplateBinding FontSize}"
|
||||
Foreground="{DynamicResource TextControlPlaceholderForeground}"
|
||||
IsHitTestVisible="False"
|
||||
Text="{TemplateBinding PlaceholderText}"
|
||||
Visibility="Collapsed" />
|
||||
<!--#endregion-->
|
||||
<ContentPresenter
|
||||
Name="PART_ContentPresenter"
|
||||
Content="{TemplateBinding SelectionBoxItem}"
|
||||
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
|
||||
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
|
||||
IsHitTestVisible="False"
|
||||
TextElement.Foreground="{TemplateBinding Foreground}" />
|
||||
</Grid>
|
||||
<!-- 可编辑时 -->
|
||||
<TextBox
|
||||
x:Name="PART_EditableTextBox"
|
||||
Grid.Column="0"
|
||||
Margin="{TemplateBinding Padding}"
|
||||
FontSize="{TemplateBinding FontSize}"
|
||||
Foreground="{TemplateBinding Foreground}"
|
||||
IsReadOnly="{TemplateBinding IsReadOnly}"
|
||||
Style="{StaticResource DefaultComboBoxTextBoxStyle}" />
|
||||
<controls:SymbolIcon
|
||||
x:Name="ChevronIcon"
|
||||
Grid.Column="1"
|
||||
Margin="{StaticResource ComboBoxChevronMargin}"
|
||||
VerticalAlignment="Center"
|
||||
FontSize="{StaticResource ComboBoxChevronSize}"
|
||||
Foreground="{DynamicResource ComboBoxDropDownGlyphForeground}"
|
||||
RenderTransformOrigin="0.5, 0.5"
|
||||
Symbol="ChevronDown24">
|
||||
<controls:SymbolIcon.RenderTransform>
|
||||
<RotateTransform Angle="0" />
|
||||
</controls:SymbolIcon.RenderTransform>
|
||||
</controls:SymbolIcon>
|
||||
<ToggleButton
|
||||
Name="ToggleButton"
|
||||
Grid.Column="0"
|
||||
Grid.ColumnSpan="2"
|
||||
Margin="0"
|
||||
ClickMode="Press"
|
||||
Focusable="False"
|
||||
Foreground="{TemplateBinding Foreground}"
|
||||
IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
|
||||
Style="{StaticResource DefaultComboBoxToggleButtonStyle}" />
|
||||
</Grid>
|
||||
<!-- 弹出式菜单 -->
|
||||
<Popup
|
||||
x:Name="PART_Popup"
|
||||
MinWidth="{TemplateBinding ActualWidth}"
|
||||
VerticalAlignment="Center"
|
||||
AllowsTransparency="True"
|
||||
Focusable="False"
|
||||
IsOpen="{TemplateBinding IsDropDownOpen}"
|
||||
Placement="{TemplateBinding Popup.Placement}"
|
||||
PopupAnimation="{TemplateBinding Popup.PopupAnimation}"
|
||||
VerticalOffset="1">
|
||||
<Border
|
||||
x:Name="DropDownBorder"
|
||||
Padding="0,4,0,6"
|
||||
Background="{DynamicResource ComboBoxDropDownBackground}"
|
||||
BorderBrush="{DynamicResource ComboBoxDropDownBorderBrush}"
|
||||
BorderThickness="1"
|
||||
CornerRadius="{DynamicResource PopupCornerRadius}"
|
||||
SnapsToDevicePixels="True">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="auto" />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid Margin="5,2,5,2" Visibility="{Binding ShowFilterBox, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisConverter}}">
|
||||
<TextBox x:Name="PART_FilterTextBox" Margin="0" />
|
||||
<TextBlock
|
||||
Name="FilterPlaceholderTextBlock"
|
||||
Padding="14,0"
|
||||
VerticalAlignment="Center"
|
||||
Foreground="{DynamicResource TextControlPlaceholderForeground}"
|
||||
IsHitTestVisible="False"
|
||||
Text="{TemplateBinding FilterBoxPlaceholderText}"
|
||||
TextBlock.FontSize="{TemplateBinding FontSize}"
|
||||
Visibility="Collapsed" />
|
||||
</Grid>
|
||||
<!--<controls:TextBox
|
||||
x:Name="PART_FilterTextBox"
|
||||
Margin="5,2,5,2"
|
||||
PlaceholderText="{TemplateBinding FilterBoxPlaceholderText}"
|
||||
Visibility="{Binding ShowFilterBox, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisConverter}}" />-->
|
||||
<controls:DynamicScrollViewer
|
||||
Grid.Row="1"
|
||||
MaxHeight="{TemplateBinding MaxDropDownHeight}"
|
||||
Margin="0"
|
||||
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
|
||||
SnapsToDevicePixels="True"
|
||||
TextElement.FontSize="{TemplateBinding FontSize}"
|
||||
TextElement.FontWeight="{TemplateBinding FontWeight}"
|
||||
TextElement.Foreground="{TemplateBinding Foreground}"
|
||||
VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
|
||||
<StackPanel
|
||||
IsItemsHost="True"
|
||||
KeyboardNavigation.DirectionalNavigation="Contained"
|
||||
TextElement.FontSize="{TemplateBinding FontSize}" />
|
||||
<!--<ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" TextElement.FontSize="{TemplateBinding FontSize}" />-->
|
||||
</controls:DynamicScrollViewer>
|
||||
</Grid>
|
||||
</Border>
|
||||
</Popup>
|
||||
</Grid>
|
||||
</Border>
|
||||
</Grid>
|
||||
</DockPanel>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsDropDownOpen" Value="True">
|
||||
<Trigger.EnterActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard>
|
||||
<DoubleAnimation
|
||||
Storyboard.TargetName="ChevronIcon"
|
||||
Storyboard.TargetProperty="(controls:SymbolIcon.RenderTransform).(RotateTransform.Angle)"
|
||||
From="0"
|
||||
To="180"
|
||||
Duration="00:00:00.167" />
|
||||
<DoubleAnimation
|
||||
Storyboard.TargetName="DropDownBorder"
|
||||
Storyboard.TargetProperty="(Border.RenderTransform).(TranslateTransform.Y)"
|
||||
From="-90"
|
||||
To="0"
|
||||
Duration="00:00:00.167">
|
||||
<DoubleAnimation.EasingFunction>
|
||||
<CircleEase EasingMode="EaseOut" />
|
||||
</DoubleAnimation.EasingFunction>
|
||||
</DoubleAnimation>
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</Trigger.EnterActions>
|
||||
<Trigger.ExitActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard>
|
||||
<DoubleAnimation
|
||||
Storyboard.TargetName="ChevronIcon"
|
||||
Storyboard.TargetProperty="(controls:SymbolIcon.RenderTransform).(RotateTransform.Angle)"
|
||||
From="180"
|
||||
To="0"
|
||||
Duration="00:00:00.167" />
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</Trigger.ExitActions>
|
||||
</Trigger>
|
||||
<Trigger SourceName="PART_FilterTextBox" Property="Text" Value="">
|
||||
<Setter TargetName="FilterPlaceholderTextBlock" Property="Visibility" Value="Visible" />
|
||||
</Trigger>
|
||||
<Trigger Property="Header" Value="{x:Null}">
|
||||
<Setter TargetName="Header_TextBlock" Property="Margin" Value="0" />
|
||||
<Setter TargetName="Header_TextBlock" Property="Visibility" Value="Collapsed" />
|
||||
</Trigger>
|
||||
<!-- 标题左右对齐时,控制最小宽度 -->
|
||||
<Trigger Property="HeaderPlacement" Value="Left">
|
||||
<Setter TargetName="Grid" Property="MinWidth" Value="80" />
|
||||
</Trigger>
|
||||
<Trigger Property="HeaderPlacement" Value="Right">
|
||||
<Setter TargetName="Grid" Property="MinWidth" Value="80" />
|
||||
</Trigger>
|
||||
<Trigger Property="HasItems" Value="False">
|
||||
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="{StaticResource ComboBoxPopupMinHeight}" />
|
||||
</Trigger>
|
||||
<Trigger SourceName="PART_Popup" Property="Popup.AllowsTransparency" Value="False">
|
||||
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="0" />
|
||||
</Trigger>
|
||||
<Trigger Property="IsGrouping" Value="True">
|
||||
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
|
||||
</Trigger>
|
||||
<Trigger Property="IsEditable" Value="True">
|
||||
<Setter Property="IsTabStop" Value="False" />
|
||||
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible" />
|
||||
<Setter TargetName="PART_ContentPresenter" Property="Visibility" Value="Hidden" />
|
||||
</Trigger>
|
||||
<MultiTrigger>
|
||||
<MultiTrigger.Conditions>
|
||||
<Condition Property="IsEnabled" Value="True" />
|
||||
<Condition Property="IsKeyboardFocusWithin" Value="True" />
|
||||
<Condition Property="IsEditable" Value="True" />
|
||||
</MultiTrigger.Conditions>
|
||||
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource ComboBoxBackgroundFocused}" />
|
||||
<Setter TargetName="AccentBorder" Property="Visibility" Value="Visible" />
|
||||
</MultiTrigger>
|
||||
<!-- 可用、指针进入、键盘不聚焦 -->
|
||||
<MultiTrigger>
|
||||
<MultiTrigger.Conditions>
|
||||
<Condition Property="IsEnabled" Value="True" />
|
||||
<Condition Property="IsMouseOver" Value="True" />
|
||||
<Condition Property="IsKeyboardFocusWithin" Value="False" />
|
||||
</MultiTrigger.Conditions>
|
||||
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource ComboBoxBackgroundPointerOver}" />
|
||||
</MultiTrigger>
|
||||
<!-- 选中项文本 -->
|
||||
<!--<MultiTrigger>
|
||||
<MultiTrigger.Conditions>
|
||||
<Condition Property="IsEditable" Value="False" />
|
||||
<Condition Property="SelectedIndex" Value="-1" />
|
||||
</MultiTrigger.Conditions>
|
||||
<Setter TargetName="PlaceholderTextBlock" Property="Visibility" Value="Visible" />
|
||||
</MultiTrigger>-->
|
||||
<!--<MultiTrigger>
|
||||
<MultiTrigger.Conditions>
|
||||
<Condition Property="PlaceholderEnable" Value="True" />
|
||||
<Condition Property="Text" Value="" />
|
||||
</MultiTrigger.Conditions>
|
||||
<Setter TargetName="PlaceholderTextBlock" Property="Visibility" Value="Visible" />
|
||||
</MultiTrigger>-->
|
||||
|
||||
<Trigger Property="Text" Value="">
|
||||
<Setter TargetName="PlaceholderTextBlock" Property="Visibility" Value="Visible" />
|
||||
</Trigger>
|
||||
<!--<Trigger Property="SelectedItem" Value="{x:Null}">
|
||||
<Setter TargetName="PlaceholderTextBlock" Property="Visibility" Value="Visible" />
|
||||
</Trigger>-->
|
||||
<!--<DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Mode=Self}}" Value="">
|
||||
<Setter TargetName="PlaceholderTextBlock" Property="Visibility" Value="Visible" />
|
||||
</DataTrigger>-->
|
||||
<!--<DataTrigger Binding="{Binding Path=SelectedItem, RelativeSource={RelativeSource AncestorType={x:Type etc:ComboBoxEx}}}" Value="{x:Null}">
|
||||
<Setter TargetName="PlaceholderTextBlock" Property="Visibility" Value="Visible" />
|
||||
</DataTrigger>-->
|
||||
<Trigger Property="IsEnabled" Value="False">
|
||||
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource ComboBoxBackgroundDisabled}" />
|
||||
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource ComboBoxBorderBrushDisabled}" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundDisabled}" />
|
||||
<!--#region Extend 水印-->
|
||||
<Setter TargetName="PlaceholderTextBlock" Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}" />
|
||||
<!--#endregion-->
|
||||
</Trigger>
|
||||
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<!--<Style BasedOn="{StaticResource DefaultComboBoxItemStyle}" TargetType="{x:Type ComboBoxItem}" />-->
|
||||
<Style BasedOn="{StaticResource DefaultComboBoxExStyle}" TargetType="{x:Type exc:ComboBoxEx}" />
|
||||
</ResourceDictionary>
|
||||
235
Wpf.Ui.Extend/Controls/ComboBoxEx/ComboBoxEx.xaml.cs
Normal file
235
Wpf.Ui.Extend/Controls/ComboBoxEx/ComboBoxEx.xaml.cs
Normal file
@@ -0,0 +1,235 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
|
||||
|
||||
namespace Wpf.Ui.Extend.Controls
|
||||
{
|
||||
[TemplatePart(Name = FilterTextBox, Type = typeof(Wpf.Ui.Controls.TextBox))]
|
||||
[TemplatePart(Name = "PART_EditableTextBox", Type = typeof(Wpf.Ui.Controls.TextBox))]
|
||||
public class ComboBoxEx : ComboBox
|
||||
{
|
||||
private const string FilterTextBox = "PART_FilterTextBox";
|
||||
static ComboBoxEx()
|
||||
{
|
||||
DefaultStyleKeyProperty.OverrideMetadata(
|
||||
typeof(ComboBoxEx),
|
||||
new FrameworkPropertyMetadata(typeof(ComboBoxEx))
|
||||
);
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>特性含义:属性面板(支持分组,支持数据绑定,支持属性提示)</para>
|
||||
/// <para>[Category("自定义分组"),Bindable(true,BindingDirection.OneWay),Description("选择框水印")]</para>
|
||||
/// </remarks>
|
||||
[Category("自定义"), Bindable(true, BindingDirection.OneWay), Description("选择框水印")]
|
||||
public string PlaceholderText
|
||||
{
|
||||
get { return (string)GetValue(PlaceholderTextProperty); }
|
||||
set { SetValue(PlaceholderTextProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty.Register(
|
||||
nameof(PlaceholderText),
|
||||
typeof(string),
|
||||
typeof(ComboBoxEx),
|
||||
new PropertyMetadata("请选择...")
|
||||
);
|
||||
|
||||
//public bool PlaceholderEnable
|
||||
//{
|
||||
// get { return (bool)GetValue(PlaceholderEnableProperty); }
|
||||
// set { SetValue(PlaceholderEnableProperty, value); }
|
||||
//}
|
||||
|
||||
//// Using a DependencyProperty as the backing store for PlaceholderEnable. This enables animation, styling, binding, etc...
|
||||
//public static readonly DependencyProperty PlaceholderEnableProperty = DependencyProperty.Register(
|
||||
// nameof(PlaceholderEnable),
|
||||
// typeof(bool),
|
||||
// typeof(ComboBoxEx),
|
||||
// new PropertyMetadata(true)
|
||||
//);
|
||||
[Category("自定义"), Bindable(true, BindingDirection.OneWay), Description("标题位置")]
|
||||
public Dock HeaderPlacement
|
||||
{
|
||||
get { return (Dock)GetValue(HeaderPlacementProperty); }
|
||||
set { SetValue(HeaderPlacementProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty HeaderPlacementProperty = DependencyProperty.Register(
|
||||
nameof(HeaderPlacement),
|
||||
typeof(Dock),
|
||||
typeof(ComboBoxEx),
|
||||
new PropertyMetadata(Dock.Top)
|
||||
);
|
||||
[Category("自定义"), Bindable(true, BindingDirection.TwoWay), Description("标题")]
|
||||
public string Header
|
||||
{
|
||||
get { return (string)GetValue(HeaderProperty); }
|
||||
set { SetValue(HeaderProperty, value); }
|
||||
}
|
||||
|
||||
// Using a DependencyProperty as the backing store for Header. This enables animation, styling, binding, etc...
|
||||
public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(
|
||||
nameof(Header),
|
||||
typeof(string),
|
||||
typeof(ComboBoxEx),
|
||||
new PropertyMetadata(null)
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// 获取或者设置下拉列表过滤文本框的显示与隐藏
|
||||
/// </summary>
|
||||
[Category("自定义"), Bindable(true, BindingDirection.OneWay), Description("过滤框")]
|
||||
public bool ShowFilterBox
|
||||
{
|
||||
get => (bool)GetValue(ShowFilterBoxProperty);
|
||||
set { SetValue(ShowFilterBoxProperty, value); }
|
||||
}
|
||||
public static readonly DependencyProperty ShowFilterBoxProperty = DependencyProperty.Register(
|
||||
nameof(ShowFilterBox),
|
||||
typeof(bool),
|
||||
typeof(ComboBoxEx),
|
||||
new PropertyMetadata(true)
|
||||
);
|
||||
[Category("自定义"), Bindable(true, BindingDirection.OneWay), Description("搜索框水印")]
|
||||
public string FilterBoxPlaceholderText
|
||||
{
|
||||
get { return (string)GetValue(FilterBoxPlaceholderTextProperty); }
|
||||
set { SetValue(FilterBoxPlaceholderTextProperty, value); }
|
||||
}
|
||||
|
||||
// Using a DependencyProperty as the backing store for FilterBoxPlaceholderText. This enables animation, styling, binding, etc...
|
||||
public static readonly DependencyProperty FilterBoxPlaceholderTextProperty = DependencyProperty.Register(
|
||||
nameof(FilterBoxPlaceholderText),
|
||||
typeof(string),
|
||||
typeof(ComboBoxEx),
|
||||
new PropertyMetadata("输入关键字")
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// 应用样板
|
||||
/// </summary>
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
base.OnApplyTemplate();
|
||||
|
||||
if (this.filterTextBox != null)
|
||||
{
|
||||
this.filterTextBox.TextChanged -= PART_FilterTextBox_TextChanged;
|
||||
}
|
||||
|
||||
this.filterTextBox = this.GetTemplateChild(FilterTextBox) as TextBox;
|
||||
if (this.filterTextBox != null)
|
||||
{
|
||||
this.filterTextBox.TextChanged += PART_FilterTextBox_TextChanged;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 过滤搜索
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void PART_FilterTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
if (this.filterTextBox == null)
|
||||
return;
|
||||
|
||||
if (view != null)
|
||||
{
|
||||
view.Filter += (o) =>
|
||||
{
|
||||
if (o is string)
|
||||
{
|
||||
var value = o.ToString().ToLower();
|
||||
return value.IndexOf(filterTextBox.Text.ToLower(), StringComparison.Ordinal) != -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
var obj = GetPropertyValue(o, this.DisplayMemberPath);
|
||||
//在xaml中写入Combobox时
|
||||
if (obj is ComboBoxItem item)
|
||||
{
|
||||
obj = item.Content;
|
||||
}
|
||||
var value = Convert.ToString(obj).ToLower();
|
||||
return value.IndexOf(filterTextBox.Text.ToLower(), StringComparison.Ordinal) != -1;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取view
|
||||
/// </summary>
|
||||
/// <param name="e"></param>
|
||||
/// <remarks>适用于在xaml中添加ComboBoxItem的情况</remarks>
|
||||
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
base.OnItemsChanged(e);
|
||||
view = CollectionViewSource.GetDefaultView(this.Items);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取view
|
||||
/// </summary>
|
||||
/// <param name="oldValue"></param>
|
||||
/// <param name="newValue"></param>
|
||||
/// <remarks>适用于绑定ItemsSource</remarks>
|
||||
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
|
||||
{
|
||||
base.OnItemsSourceChanged(oldValue, newValue);
|
||||
|
||||
view = CollectionViewSource.GetDefaultView(this.ItemsSource);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取属性,若无法获取则返回源对象
|
||||
/// </summary>
|
||||
public static object GetPropertyValue(object obj, string path)
|
||||
{
|
||||
if (obj == null)
|
||||
return string.Empty;
|
||||
|
||||
var result = obj;
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
{
|
||||
var property = obj.GetType().GetProperty(path);
|
||||
if (property != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = property.GetValue(obj, null);
|
||||
return result;
|
||||
}
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
var type = obj.GetType();
|
||||
var mInfo = type.GetMethod("get_" + path);
|
||||
if (mInfo != null)
|
||||
{
|
||||
result = mInfo.Invoke(obj, null);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private TextBox filterTextBox;
|
||||
private ICollectionView view;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user