Files
Shrlalgo.RvKits/WPFluent/Controls/CheckComboBox/CheckComboBox.cs

300 lines
9.6 KiB
C#
Raw Normal View History


using WPFluent.Extensions;
2024-09-22 11:05:41 +08:00
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
2024-09-22 11:05:41 +08:00
using System.Windows.Data;
namespace WPFluent.Controls
2024-09-22 11:05:41 +08:00
{
/// <summary>
/// 通过该类,对绑定的集合->重新创建为CheckComboBoxItem->应用默认样式
/// </summary>
public class CheckComboBox : ComboBoxEx
{
#region Internal
/// <summary>
/// 行选中
/// </summary>
/// <param name="item"></param>
internal void NotifyCheckComboBoxItemClicked(CheckComboBoxItem item)
{
var itemContent = Convert.ToString(item.Content);
SelectedStrList ??= [];
SelectedObjList ??= [];
if(item.IsSelected)
{
if(!SelectedStrList.Contains(item.Content))
{
SelectedStrList.Add(itemContent);
}
if(!SelectedObjList.Contains(item.DataContext))
{
SelectedObjList.Add(item.DataContext);
}
}
else
{
if(SelectedStrList.Contains(itemContent))
{
SelectedStrList.Remove(itemContent);
}
if(SelectedObjList.Contains(item.DataContext))
{
SelectedObjList.Remove(item.DataContext);
}
}
SetCheckComboBoxValueAndContent();
}
#endregion
2024-09-22 11:05:41 +08:00
#region PrivateProperty
private ContentPresenter PART_ContentSite;
private TextBox PART_FilterTextBox;
private ICollectionView view;
private Popup PART_Popup;
private bool popupIsFirstOpen;
#endregion
#region DependencyProperty
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register(
nameof(Content),
typeof(string),
typeof(CheckComboBox),
new PropertyMetadata(string.Empty));
2024-09-22 11:05:41 +08:00
/// <summary>
/// 内容
/// </summary>
public string Content
{
get { return (string)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
nameof(Value),
typeof(string),
typeof(CheckComboBox),
new PropertyMetadata(string.Empty));
2024-09-22 11:05:41 +08:00
/// <summary>
/// 值
/// </summary>
public string Value { get { return (string)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } }
2024-09-22 11:05:41 +08:00
#region SelectedObjList
[Bindable(true), Description("获取或者设置当前勾选项列表")]
public ObservableCollection<object> SelectedObjList
{
get { return (ObservableCollection<object>)GetValue(SelectedObjListProperty); }
set { SetValue(SelectedObjListProperty, value); }
}
public static readonly DependencyProperty SelectedObjListProperty =
DependencyProperty.Register(
nameof(SelectedObjList),
typeof(ObservableCollection<object>),
typeof(CheckComboBox),
new PropertyMetadata(null, OnSelectedObjListChanged));
2024-09-22 11:05:41 +08:00
private static void OnSelectedObjListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var checkComboBox = d as CheckComboBox;
if(checkComboBox.ItemsSource != null)
2024-09-22 11:05:41 +08:00
checkComboBox.SetCheckComboBoxValueAndContent();
}
#endregion
#region SelectedStrList
public ObservableCollection<string> SelectedStrList
{
get { return (ObservableCollection<string>)GetValue(SelectedStrListProperty); }
set { SetValue(SelectedStrListProperty, value); }
}
public static readonly DependencyProperty SelectedStrListProperty =
DependencyProperty.Register(
nameof(SelectedStrList),
typeof(ObservableCollection<string>),
typeof(CheckComboBox));
2024-09-22 11:05:41 +08:00
#endregion
/// <summary>
/// 获取或者设置最多显示的选中个数
/// </summary>
public int MaxShowNumber
{
get { return (int)GetValue(MaxShowNumberProperty); }
set { SetValue(MaxShowNumberProperty, value); }
}
public static readonly DependencyProperty MaxShowNumberProperty =
DependencyProperty.Register(
nameof(MaxShowNumber),
typeof(int),
typeof(CheckComboBox),
new PropertyMetadata(3));
2024-09-22 11:05:41 +08:00
#endregion
#region Constructors
static CheckComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(CheckComboBox),
new FrameworkPropertyMetadata(typeof(CheckComboBox)));
2024-09-22 11:05:41 +08:00
}
2024-09-22 11:05:41 +08:00
public CheckComboBox()
{
SelectedObjList = [];
SelectedStrList = [];
2024-09-22 11:05:41 +08:00
}
#endregion
#region Override
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if(PART_FilterTextBox != null)
2024-09-22 11:05:41 +08:00
{
PART_FilterTextBox.TextChanged -= PART_FilterTextBox_TextChanged;
2024-09-22 11:05:41 +08:00
}
if(PART_Popup != null)
2024-09-22 11:05:41 +08:00
{
PART_Popup.Opened -= PART_Popup_Opened;
2024-09-22 11:05:41 +08:00
}
PART_ContentSite = GetTemplateChild("PART_ContentSite") as ContentPresenter;
PART_FilterTextBox = GetTemplateChild("PART_FilterTextBox") as TextBox;
PART_Popup = GetTemplateChild("PART_Popup") as Popup;
if(PART_FilterTextBox != null)
2024-09-22 11:05:41 +08:00
{
PART_FilterTextBox.TextChanged += PART_FilterTextBox_TextChanged;
2024-09-22 11:05:41 +08:00
}
view = CollectionViewSource.GetDefaultView(ItemsSource);
if(PART_Popup != null)
2024-09-22 11:05:41 +08:00
{
PART_Popup.Opened += PART_Popup_Opened;
2024-09-22 11:05:41 +08:00
}
Init();
2024-09-22 11:05:41 +08:00
}
2024-09-22 11:05:41 +08:00
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
if(item is not CheckComboBoxItem)
2024-09-22 11:05:41 +08:00
{
if(element is CheckComboBoxItem checkComboBoxItem && !string.IsNullOrEmpty(DisplayMemberPath))
2024-09-22 11:05:41 +08:00
{
var binding = new Binding(DisplayMemberPath);
2024-09-22 11:05:41 +08:00
checkComboBoxItem.SetBinding(CheckComboBoxItem.ContentProperty, binding);
}
}
base.PrepareContainerForItemOverride(element, item);
}
protected override DependencyObject GetContainerForItemOverride() { return new CheckComboBoxItem(); }
2024-09-22 11:05:41 +08:00
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is CheckComboBoxItem;
2024-09-22 11:05:41 +08:00
}
#endregion
#region PrivateFunction
private void Init()
{
popupIsFirstOpen = true;
2024-09-22 11:05:41 +08:00
if(SelectedObjList != null)
2024-09-22 11:05:41 +08:00
{
foreach(var obj in SelectedObjList)
2024-09-22 11:05:41 +08:00
{
if(string.IsNullOrWhiteSpace(DisplayMemberPath))
2024-09-22 11:05:41 +08:00
{
SelectedStrList.Add(obj.ToString());
2024-09-22 11:05:41 +08:00
}
else
{
SelectedStrList.Add(obj.GetPropertyValue(DisplayMemberPath).ToString());
2024-09-22 11:05:41 +08:00
}
}
}
SetCheckComboBoxValueAndContent();
2024-09-22 11:05:41 +08:00
}
private void SetCheckComboBoxValueAndContent()
{
if(SelectedObjList == null || SelectedObjList.Count <= 0)
2024-09-22 11:05:41 +08:00
SelectedStrList = [];
if(SelectedStrList == null)
return;
2024-09-22 11:05:41 +08:00
Content = SelectedStrList.Count > MaxShowNumber
? $"选中{SelectedStrList.Count}个"
: SelectedStrList.Aggregate(string.Empty, (current, p) => $"{current}{p}, ")
.TrimEnd(new char[] { ' ' })
.TrimEnd(new char[] { ',' });
2024-09-22 11:05:41 +08:00
Value = SelectedStrList.Aggregate(string.Empty, (current, p) => $"{current}{p},")
.TrimEnd(new char[] { ',' });
2024-09-22 11:05:41 +08:00
}
#endregion
#region Event Implement Function
/// <summary>
/// 每次Open回显数据不太好先这么处理
/// </summary>
private void PART_Popup_Opened(object sender, EventArgs e)
{
if(!popupIsFirstOpen)
return;
2024-09-22 11:05:41 +08:00
popupIsFirstOpen = false;
2024-09-22 11:05:41 +08:00
if(ItemsSource == null || SelectedObjList == null)
return;
2024-09-22 11:05:41 +08:00
foreach(var obj in SelectedObjList)
2024-09-22 11:05:41 +08:00
{
if(ItemContainerGenerator.ContainerFromItem(obj) is CheckComboBoxItem checkComboBoxItem)
2024-09-22 11:05:41 +08:00
checkComboBoxItem.IsSelected = true;
}
}
2024-09-22 11:05:41 +08:00
/// <summary>
/// 搜索关键字
/// </summary>
private void PART_FilterTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if(PART_FilterTextBox == null || view == null)
return;
2024-09-22 11:05:41 +08:00
view.Filter += (o) =>
{
var value = Convert.ToString(o.GetPropertyValue(DisplayMemberPath)).ToLower();
return value.Contains(PART_FilterTextBox.Text);
2024-09-22 11:05:41 +08:00
};
foreach(var obj in SelectedObjList)
2024-09-22 11:05:41 +08:00
{
if(ItemContainerGenerator.ContainerFromItem(obj) is CheckComboBoxItem checkComboBoxItem)
2024-09-22 11:05:41 +08:00
checkComboBoxItem.IsSelected = true;
}
}
#endregion
}
}