using System;
using System.Collections.Generic;
using System.Text;
using CPF.Drawing;
using CPF.Input;
using System.Linq;
using CPF.Animation;
using System.Collections;
using System.ComponentModel;
namespace CPF.Controls
{
///
/// 表示带有下拉列表的选择控件,通过单击控件上的箭头可显示或隐藏下拉列表
///
[Description("表示带有下拉列表的选择控件,通过单击控件上的箭头可显示或隐藏下拉列表")]
public class ComboBox : ListBox
{
static Popup popup;
public ComboBox()
{
//DropDownPanel.LayoutUpdated += DropDownPanel_LayoutUpdated;
}
//private void DropDownPanel_LayoutUpdated(object sender, RoutedEventArgs e)
//{
// if (IsDropDownOpen && popup != null)
// {
// var s = DropDownPanel.ActualSize;
// BeginInvoke(() =>
// {
// popup.Width = s.Width + DropDownPanel.MarginLeft.GetActualValue(s.Width) + DropDownPanel.MarginRight.GetActualValue(s.Width) + 1;
// });
// //popup.Height = s.Height + DropDownPanel.MarginTop.GetActualValue(s.Height) + DropDownPanel.MarginBottom.GetActualValue(s.Height);
// //System.Diagnostics.Debug.WriteLine(popup.Width);
// }
//}
///
/// 下拉框容器 #DropDownPanel
///
[NotCpfProperty]
public Panel DropDownPanel { get; private set; } = new Panel { Name = "DropDownPanel" };
///
/// 获取或设置一个值,该值指示组合框的下拉部分当前是否打开
///
public bool IsDropDownOpen { get { return GetValue(); } set { SetValue(value); } }
/////
///// 获取选择框内容的项模板
/////
//public UIElementTemplate SelectionItemTemplate
//{
// get { return GetValue>(); }
// set { SetValue(value); }
//}
//SelectionItem selectionItem;
protected override void InitializeComponent()
{
Children.Add(new TextBlock
{
MarginLeft = 3,
MarginRight = 15,
VerticalAlignment = VerticalAlignment.Center,
MaxHeight = "100%",
TextTrimming = TextTrimming.CharacterEllipsis,
Bindings =
{
{nameof(Visibility),nameof(IsEditable),this,BindingMode.OneWay, (bool e)=>e?Visibility.Collapsed: Visibility.Visible },
{nameof(TextBlock.Text),nameof(SelectedItems),this,BindingMode.OneWay,SelectedValueConvert}
}
});
Children.Add(new TextBox
{
MarginLeft = 3,
MarginRight = 15,
AcceptsReturn = false,
HScrollBarVisibility = ScrollBarVisibility.Hidden,
VScrollBarVisibility = ScrollBarVisibility.Hidden,
Bindings =
{
{nameof(Visibility),nameof(IsEditable),this,BindingMode.OneWay, (bool e)=>e?Visibility.Visible: Visibility.Collapsed },
{nameof(TextBlock.Text),nameof(SelectedItems),this,BindingMode.OneWay,SelectedItemsToText }
},
Commands =
{
{nameof(TextBox.Text),TextBoxTextToSelectValue }
}
});
Children.Add(new Shapes.Polyline { MarginRight = 5, IsAntiAlias = true, Points = { new Point(), new Point(4, 4), new Point(8, 0) } });
//DropDownPanel.MarginTop = 1;
DropDownPanel.MarginBottom = 1;
//DropDownPanel.MarginLeft = 1;
//DropDownPanel.MarginRight = 1;
DropDownPanel.MinHeight = 20;
//DropDownPanel.MaxHeight = 300;
//DropDownPanel.Background = "#fff";
//DropDownPanel.MaxWidth = 500;
DropDownPanel[nameof(Width)] = (this, nameof(ActualSize), a => (FloatField)((Size)a).Width);
var panel = ItemsPanel.CreateElement();
panel.Name = "itemsPanel";
panel.PresenterFor = this;
panel.Width = "100%";
panel.MarginTop = 0;
//panel.Height = "100%";
if (ShowClear)
{
if (IsVirtualizing)
{
DropDownPanel.Children.Add(new Border
{
Name = "dropDownBorder",
MarginTop = 0,
Width = "100%",
Height = "100%",
BorderFill = "172,172,172",
Background = "#fff",
Children =
{
new ScrollViewer { MarginTop = 0, MarginBottom = 30, Width = "100%", MaxHeight = 300, Content = new VirtualizationPresenter { Width = "100%", MarginTop = 0, Child = panel, PresenterFor = this }, } ,
new Panel
{
MarginBottom = 0,
Width = "100%",
Height = 30,
Children =
{
new TextBlock {
Classes = "lblgreen",
Foreground = "#05AA69",
Cursor = Cursors.Hand, Name = "清除",Text = "清除",
Commands = {
{ nameof(TextBlock.MouseUp),
nameof(ClearSelectedVale),
this
}
} }
}
}
}
});
}
else
{
DropDownPanel.Children.Add(new Border
{
Name = "dropDownBorder",
MarginTop = 0,
Width = "100%",
Height = "100%",
BorderFill = "172,172,172",
Background = "#fff",
Children = {
new ScrollViewer { MarginTop = 0, Width = "100%", MarginBottom = 30, Content = panel, MaxHeight = 300 },
new Panel
{
MarginBottom = 0,
Width = "100%",
Height = 30,
Children =
{
new TextBlock {
Classes = "lblgreen",
Foreground = "#05AA69",
Cursor = Cursors.Hand, Name = "清除",Text = "清除",
Commands = {
{ nameof(TextBlock.MouseUp),
nameof(ClearSelectedVale),
this
}
} }
}
}
}
});
}
}
else
{
if (IsVirtualizing)
{
DropDownPanel.Children.Add(new Border { Name = "dropDownBorder", MarginTop = 0, Width = "100%", Height = "100%", BorderFill = "172,172,172", Background = "#fff", Child = new ScrollViewer { MarginTop = 0, Height = "100%", Width = "100%", MaxHeight = 300, Content = new VirtualizationPresenter { Width = "100%", MarginTop = 0, Child = panel, PresenterFor = this }, } });
}
else
{
DropDownPanel.Children.Add(new Border { Name = "dropDownBorder", MarginTop = 0, Width = "100%", Height = "100%", BorderFill = "172,172,172", Background = "#fff", Child = new ScrollViewer { MarginTop = 0, Width = "100%", Content = panel, MaxHeight = 300 } });
}
}
}
private void ClearSelectedVale()
{
SelectedValue = null;
Popup.Hide();
IsDropDownOpen = false;
}
protected virtual string SelectedValueConvert(object data)
{
var display = DisplayMemberPath;
StringBuilder sb = new StringBuilder();
if (data is IEnumerable enumerable)
{
foreach (var item in enumerable)
{
sb.Append(ConvertStr(item, display));
sb.Append(',');
}
if (sb.Length > 0 && sb[sb.Length - 1] == ',')
{
sb.Remove(sb.Length - 1, 1);
}
}
else if (data != null)
{
sb.Append(ConvertStr(data, display));
}
return sb.ToString();
}
string ConvertStr(object a, string display)
{
var value = a;
if (a is ContentControl contentControl)
{
value = contentControl.Content;
}
else if (a is UIElement element)
{
value = element.DataContext;
}
if (value == null)
{
return "";
}
if (!string.IsNullOrWhiteSpace(display))
{
var v = value.GetPropretyValue(display);
if (v != null)
{
return v.ToString();
}
return "";
}
return value.ToString();
}
protected virtual string SelectedItemsToText(object data)
{
//if (data == null || data is IEnumerable