* UICombobox: 更新了数据绑定相关代码
This commit is contained in:
parent
8aeca1b7c3
commit
d291e4f96e
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -25,7 +25,6 @@ using System.ComponentModel;
|
|||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Drawing.Design;
|
using System.Drawing.Design;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using static System.Windows.Forms.ComboBox;
|
|
||||||
|
|
||||||
namespace Sunny.UI
|
namespace Sunny.UI
|
||||||
{
|
{
|
||||||
@ -38,10 +37,10 @@ namespace Sunny.UI
|
|||||||
public UIComboBox()
|
public UIComboBox()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
box.SelectedIndexChanged += Box_SelectedIndexChanged;
|
ListBox.SelectedIndexChanged += Box_SelectedIndexChanged;
|
||||||
box.DataSourceChanged += Box_DataSourceChanged;
|
ListBox.DataSourceChanged += Box_DataSourceChanged;
|
||||||
box.DisplayMemberChanged += Box_DisplayMemberChanged;
|
ListBox.DisplayMemberChanged += Box_DisplayMemberChanged;
|
||||||
box.ValueMemberChanged += Box_ValueMemberChanged;
|
ListBox.ValueMemberChanged += Box_ValueMemberChanged;
|
||||||
DropDownWidth = 150;
|
DropDownWidth = 150;
|
||||||
fullControlSelect = true;
|
fullControlSelect = true;
|
||||||
}
|
}
|
||||||
@ -63,8 +62,7 @@ namespace Sunny.UI
|
|||||||
|
|
||||||
private void Box_SelectedIndexChanged(object sender, EventArgs e)
|
private void Box_SelectedIndexChanged(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
Text = box.GetItemText(box.SelectedItem);
|
Text = ListBox.GetItemText(ListBox.SelectedItem);
|
||||||
GetSelectedValue();
|
|
||||||
SelectedValueChanged?.Invoke(this, e);
|
SelectedValueChanged?.Invoke(this, e);
|
||||||
SelectedIndexChanged?.Invoke(this, e);
|
SelectedIndexChanged?.Invoke(this, e);
|
||||||
}
|
}
|
||||||
@ -79,8 +77,6 @@ namespace Sunny.UI
|
|||||||
|
|
||||||
public event EventHandler SelectedValueChanged;
|
public event EventHandler SelectedValueChanged;
|
||||||
|
|
||||||
public readonly ComboBox box = new ComboBox();
|
|
||||||
|
|
||||||
protected override void ItemForm_ValueChanged(object sender, object value)
|
protected override void ItemForm_ValueChanged(object sender, object value)
|
||||||
{
|
{
|
||||||
if (SelectedIndex != ListBox.SelectedIndex)
|
if (SelectedIndex != ListBox.SelectedIndex)
|
||||||
@ -131,18 +127,6 @@ namespace Sunny.UI
|
|||||||
|
|
||||||
private void UIComboBox_ButtonClick(object sender, EventArgs e)
|
private void UIComboBox_ButtonClick(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
ListBox.Items.Clear();
|
|
||||||
if (Items.Count == 0 || ItemForm.Visible)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var data in Items)
|
|
||||||
{
|
|
||||||
ListBox.Items.Add(GetItemText(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
ListBox.SelectedIndex = SelectedIndex;
|
|
||||||
ItemForm.Show(this, new Size(DropDownWidth < Width ? Width : DropDownWidth, CalcItemFormHeight()));
|
ItemForm.Show(this, new Size(DropDownWidth < Width ? Width : DropDownWidth, CalcItemFormHeight()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,60 +138,10 @@ namespace Sunny.UI
|
|||||||
ListBox.SetStyleColor(uiColor);
|
ListBox.SetStyleColor(uiColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
private object dataSource;
|
|
||||||
|
|
||||||
[DefaultValue(null), RefreshProperties(RefreshProperties.Repaint), AttributeProvider(typeof(IListSource))]
|
|
||||||
[Description("数据源"), Category("SunnyUI")]
|
|
||||||
public object DataSource
|
public object DataSource
|
||||||
{
|
{
|
||||||
get => dataSource;
|
get => ListBox.DataSource;
|
||||||
set
|
set => ListBox.DataSource = value;
|
||||||
{
|
|
||||||
SetDataConnection(value, new BindingMemberInfo(DisplayMember));
|
|
||||||
dataSource = value;
|
|
||||||
box.Items.Clear();
|
|
||||||
|
|
||||||
if (dataManager != null)
|
|
||||||
{
|
|
||||||
foreach (var obj in dataManager.List)
|
|
||||||
{
|
|
||||||
box.Items.Add(obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool inSetDataConnection;
|
|
||||||
private CurrencyManager dataManager;
|
|
||||||
|
|
||||||
private void SetDataConnection(object newDataSource, BindingMemberInfo newDisplayMember)
|
|
||||||
{
|
|
||||||
bool dataSourceChanged = dataSource != newDataSource;
|
|
||||||
bool displayMemberChanged = !DisplayMember.Equals(newDisplayMember.BindingPath);
|
|
||||||
|
|
||||||
if (inSetDataConnection)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
inSetDataConnection = true;
|
|
||||||
if (dataSourceChanged || displayMemberChanged)
|
|
||||||
{
|
|
||||||
CurrencyManager newDataManager = null;
|
|
||||||
if (newDataSource != null && newDataSource != Convert.DBNull)
|
|
||||||
{
|
|
||||||
newDataManager = (CurrencyManager)BindingContext[newDataSource, newDisplayMember.BindingPath];
|
|
||||||
}
|
|
||||||
|
|
||||||
dataManager = newDataManager;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
inSetDataConnection = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[DefaultValue(150)]
|
[DefaultValue(150)]
|
||||||
@ -215,29 +149,26 @@ namespace Sunny.UI
|
|||||||
public int DropDownWidth { get; set; }
|
public int DropDownWidth { get; set; }
|
||||||
|
|
||||||
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
|
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
|
||||||
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
|
|
||||||
[Localizable(true)]
|
[Localizable(true)]
|
||||||
|
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
|
||||||
[MergableProperty(false)]
|
[MergableProperty(false)]
|
||||||
[Description("下拉显示项"), Category("SunnyUI")]
|
[Description("列表项"), Category("SunnyUI")]
|
||||||
public ObjectCollection Items
|
public ListBox.ObjectCollection Items => ListBox.Items;
|
||||||
{
|
|
||||||
get => box.Items;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
||||||
[Description("选中索引"), Category("SunnyUI")]
|
[Description("选中索引"), Category("SunnyUI")]
|
||||||
public int SelectedIndex
|
public int SelectedIndex
|
||||||
{
|
{
|
||||||
get => box.SelectedIndex;
|
get => ListBox.SelectedIndex;
|
||||||
set => box.SelectedIndex = value;
|
set => ListBox.SelectedIndex = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Browsable(false), Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
[Browsable(false), Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
||||||
[Description("选中项"), Category("SunnyUI")]
|
[Description("选中项"), Category("SunnyUI")]
|
||||||
public object SelectedItem
|
public object SelectedItem
|
||||||
{
|
{
|
||||||
get => box.SelectedItem;
|
get => ListBox.SelectedItem;
|
||||||
set => box.SelectedItem = value;
|
set => ListBox.SelectedItem = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
||||||
@ -268,12 +199,8 @@ namespace Sunny.UI
|
|||||||
[TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
|
[TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
|
||||||
public string DisplayMember
|
public string DisplayMember
|
||||||
{
|
{
|
||||||
get => box.DisplayMember;
|
get => ListBox.DisplayMember;
|
||||||
set
|
set => ListBox.DisplayMember = value;
|
||||||
{
|
|
||||||
SetDataConnection(dataSource, new BindingMemberInfo(value));
|
|
||||||
box.DisplayMember = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Description("获取或设置指示显示值的方式的格式说明符字符。"), Category("SunnyUI")]
|
[Description("获取或设置指示显示值的方式的格式说明符字符。"), Category("SunnyUI")]
|
||||||
@ -282,16 +209,16 @@ namespace Sunny.UI
|
|||||||
[MergableProperty(false)]
|
[MergableProperty(false)]
|
||||||
public string FormatString
|
public string FormatString
|
||||||
{
|
{
|
||||||
get => box.FormatString;
|
get => ListBox.FormatString;
|
||||||
set => box.FormatString = value;
|
set => ListBox.FormatString = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Description("获取或设置指示显示值是否可以进行格式化操作。"), Category("SunnyUI")]
|
[Description("获取或设置指示显示值是否可以进行格式化操作。"), Category("SunnyUI")]
|
||||||
[DefaultValue(false)]
|
[DefaultValue(false)]
|
||||||
public bool FormattingEnabled
|
public bool FormattingEnabled
|
||||||
{
|
{
|
||||||
get => box.FormattingEnabled;
|
get => ListBox.FormattingEnabled;
|
||||||
set => box.FormattingEnabled = value;
|
set => ListBox.FormattingEnabled = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Description("获取或设置要为此列表框实际值的属性。"), Category("SunnyUI")]
|
[Description("获取或设置要为此列表框实际值的属性。"), Category("SunnyUI")]
|
||||||
@ -299,22 +226,8 @@ namespace Sunny.UI
|
|||||||
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
|
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
|
||||||
public string ValueMember
|
public string ValueMember
|
||||||
{
|
{
|
||||||
get => box.ValueMember;
|
get => ListBox.ValueMember;
|
||||||
set => box.ValueMember = value;
|
set => ListBox.ValueMember = value;
|
||||||
}
|
|
||||||
|
|
||||||
private object selectedValue;
|
|
||||||
|
|
||||||
private void GetSelectedValue()
|
|
||||||
{
|
|
||||||
if (SelectedIndex != -1 && dataManager != null)
|
|
||||||
{
|
|
||||||
selectedValue = FilterItemOnProperty(SelectedItem, ValueMember);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
selectedValue = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[
|
[
|
||||||
@ -325,73 +238,13 @@ namespace Sunny.UI
|
|||||||
]
|
]
|
||||||
public object SelectedValue
|
public object SelectedValue
|
||||||
{
|
{
|
||||||
get
|
get => ListBox.SelectedValue;
|
||||||
{
|
set => ListBox.SelectedValue = value;
|
||||||
GetSelectedValue();
|
|
||||||
return selectedValue;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
selectedValue = value;
|
|
||||||
|
|
||||||
if (value == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dataManager != null)
|
|
||||||
{
|
|
||||||
int index = DataManagerFind(value);
|
|
||||||
SelectedIndex = index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int DataManagerFind(object value)
|
|
||||||
{
|
|
||||||
if (dataManager == null) return -1;
|
|
||||||
for (int i = 0; i < dataManager.List.Count; i++)
|
|
||||||
{
|
|
||||||
var item = dataManager.List[i];
|
|
||||||
if (FilterItemOnProperty(item, ValueMember).Equals(value))
|
|
||||||
{
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private object FilterItemOnProperty(object item, string field)
|
|
||||||
{
|
|
||||||
if (item != null && field.Length > 0)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// if we have a dataSource, then use that to display the string
|
|
||||||
PropertyDescriptor descriptor;
|
|
||||||
if (dataManager != null)
|
|
||||||
descriptor = dataManager.GetItemProperties().Find(field, true);
|
|
||||||
else
|
|
||||||
descriptor = TypeDescriptor.GetProperties(item).Find(field, true);
|
|
||||||
|
|
||||||
if (descriptor != null)
|
|
||||||
{
|
|
||||||
item = descriptor.GetValue(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return item;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetItemText(object item)
|
public string GetItemText(object item)
|
||||||
{
|
{
|
||||||
return box.GetItemText(item);
|
return ListBox.GetItemText(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user