LSComboBoxItem.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Longshine.SLLib.InputControls.LSComboBox.LSNullableComboBox
{
public class LSComboBoxItem
{
/// <summary>
/// 显示字段 (DisplayMemberPath)
/// </summary>
public string DisplayString { get; set; }
/// <summary>
/// 选中项 (SelectedValuePath)
/// </summary>
public object ItemValue { get; set; }
}
}
LSComboBoxItemList.cs
using System.Collections.Generic;
namespace Longshine.SLLib.InputControls.LSComboBox.LSNullableComboBox
{
public class ComboBoxItemList<T> : List<LSComboBoxItem>
{
public delegate string GetDisplayString(T t);
public ComboBoxItemList(bool addEmptyItem, IEnumerable<T> items, GetDisplayString getDisplayString)
{
if (addEmptyItem)
{
Add(new LSComboBoxItem { DisplayString = "------", ItemValue = null });
}
if (items != null)
{
foreach (T item in items)
{
Add(new LSComboBoxItem { DisplayString = getDisplayString(item), ItemValue = item });
}
}
}
}
}
LSEnhancedComboBox.cs
using System.Windows;
using System.Windows.Controls;
namespace Longshine.SLLib.InputControls
{
/// <summary>
/// 有默认空值的下拉框
/// </summary>
public class LSEnhancedComboBox : ComboBox
{
public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(LSEnhancedComboBox), new PropertyMetadata(new PropertyChangedCallback(SelectedValuePathPropertyChanged)));
public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(LSEnhancedComboBox), new PropertyMetadata(new PropertyChangedCallback(SelectedValuePropertyChanged)));
public LSEnhancedComboBox()
: base()
{
this.DisplayMemberPath = "DisplayString";
this.SelectedValuePath = "ItemValue";
base.SelectionChanged += new SelectionChangedEventHandler(ComboBoxClassic_SelectionChanged);
}
protected void ComboBoxClassic_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (SelectedItem != null && !string.IsNullOrEmpty(SelectedValuePath))
{
SetValue(LSEnhancedComboBox.SelectedValueProperty, SelectedItem.GetType().GetProperty(SelectedValuePath).GetValue(SelectedItem, null));
}
}
static void SelectedValuePathPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
LSEnhancedComboBox cmb = (LSEnhancedComboBox)d;
foreach (var item in cmb.Items)
{
var currentValue = item.GetType().GetProperty(cmb.SelectedValuePath).GetValue(item, null);
if (currentValue == null && e.NewValue == null)
{
cmb.SelectedItem = item;
return;
}
if (currentValue != null)
{
if (currentValue.Equals(e.NewValue))
{
cmb.SelectedItem = item;
return;
}
}
}
cmb.SelectedIndex = -1;
}
public string SelectedValuePath
{
get { return (string)GetValue(LSEnhancedComboBox.SelectedValuePathProperty); }
set { SetValue(LSEnhancedComboBox.SelectedValuePathProperty, value); }
}
public object SelectedValue
{
get { return GetValue(SelectedValueProperty); }
set { SetValue(LSEnhancedComboBox.SelectedValueProperty, value); }
}
}
}
使用:
<inputcontrols:LSEnhancedComboBox x:Name="cbRYType" Grid.Column="1" Grid.Row="1"/>
void cbRYType_Loaded(object sender, RoutedEventArgs e)
{
var ws = WcfServiceClientFactory<ComServiceClient, ComService>.CreateServiceClient();
ws.GetCodeTableDataCompleted += (ss, e2) =>
{
if (e2.Error != null)
System.Windows.Browser.HtmlPage.Window.Alert(e2.Error.Message);
else
{
if (e2.Result == null) return;
ComboBoxItemList<CodeEntity> dataList = new ComboBoxItemList<CodeEntity>(true,
e2.Result,
c => c.MC0000);
this.cbRYType.ItemsSource = dataList;
this.cbRYType.SelectedIndex = 0;
}
};
ws.GetCodeTableDataAsync("BM_KK");
}
界面:

2520

被折叠的 条评论
为什么被折叠?



