silverlight combox 绑定 source 时, 处理空值问题。
来源:互联网 发布:华信软件技术有限公司 编辑:程序博客网 时间:2024/06/05 10:21
参考 : http://www.codeproject.com/Articles/202523/Silverlight-Combobox
public class ComboBoxHelper
{
private static object _SyncLock = new object();
private static Dictionary<int, Binding> _BindingsByHashCode = new Dictionary<int, Binding>();
/// <summary>
/// Searches for all ComboBoxes below the provided element and subscribes them to the binding patch solution
/// </summary>
/// <param name="rootElement">The root UI element to search</param>
public static void FixSelectedValueBindings(UIElement rootElement)
{
List<ComboBox> comboBoxes = new List<ComboBox>();
GetAllComboBoxes(rootElement, comboBoxes);
foreach (ComboBox comboBox in comboBoxes)
{
FixSelectedValueBindings(comboBox);
}
}
/// <summary>
/// Subscribes the supplied combobox to the binding patch solution
/// </summary>
/// <param name="rootElement">The root UI element to search</param>
public static void FixSelectedValueBindings(ComboBox comboBox)
{
if (!IsControlRegistered(comboBox))
{
//subscribe to events
comboBox.SelectionChanged += new SelectionChangedEventHandler(comboBox_SelectionChanged);
comboBox.Unloaded += new RoutedEventHandler(comboBox_Unloaded);
//save the initial binding
SaveBinding(comboBox);
}
}
/// <summary>
/// Traveses the UI tree recursively and finds all comboboxes
/// </summary>
/// <param name="element">The root of the ui tree</param>
/// <param name="comboBoxes">The list of comboxes, this list should be empty initially and will be populated recursively.</param>
private static void GetAllComboBoxes(UIElement element, List<ComboBox> comboBoxes)
{
if (element == null)
return;
int childCount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < childCount; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(element,i);
if (child is ComboBox)
{
comboBoxes.Add((ComboBox)child);
}
else
{
GetAllComboBoxes(child, comboBoxes);
}
}
}
/// <summary>
/// Event handler subscribed to all comboxes participating in this patch
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
//if the selected value is being set to null
if (comboBox.SelectedValue == null)
{
//get the saved binding and re-apply it
Binding binding = GetSavedBinding(comboBox.GetHashCode());
if (binding != null)
{
comboBox.SetBinding(ComboBox.SelectedValueProperty, binding);
}
}
else
{
//if the binding is not null, save it to the cache
SaveBinding(comboBox);
}
}
/// <summary>
/// Checks if a combox is already participating in the solution. Comboxes are identified by hashcode.
/// </summary>
/// <param name="comboBox"></param>
/// <returns></returns>
private static bool IsControlRegistered(ComboBox comboBox)
{
lock (_SyncLock)
{
return _BindingsByHashCode.ContainsKey(comboBox.GetHashCode());
}
}
/// <summary>
/// Combobox Unload event handler, unsubscribes the combox from our events and clears it out of the cache.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void comboBox_Unloaded(object sender, RoutedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
comboBox.Unloaded -= comboBox_Unloaded;
comboBox.SelectionChanged -= comboBox_SelectionChanged;
RemoveSavedBinding(comboBox.GetHashCode());
}
private static void RemoveSavedBinding(int objectHashCode)
{
lock (_SyncLock)
{
if (_BindingsByHashCode.ContainsKey(objectHashCode))
{
_BindingsByHashCode.Remove(objectHashCode);
}
}
}
private static void SaveBinding(ComboBox comboBox)
{
BindingExpression bindingExpression = comboBox.GetBindingExpression(ComboBox.SelectedValueProperty);
if (bindingExpression != null)
{
SaveBinding(comboBox.GetHashCode(), bindingExpression.ParentBinding);
}
}
private static void SaveBinding(int objectHashCode, Binding binding)
{
if (binding == null)
return;
lock (_SyncLock)
{
if (_BindingsByHashCode.ContainsKey(objectHashCode))
{
_BindingsByHashCode[objectHashCode] = binding;
}
else
{
_BindingsByHashCode.Add(objectHashCode, binding);
}
}
}
private static Binding GetSavedBinding(int objectHashCode)
{
Binding binding = null;
lock (_SyncLock)
{
if (_BindingsByHashCode.ContainsKey(objectHashCode))
{
binding = _BindingsByHashCode[objectHashCode];
}
}
return binding;
}
}
然后在有 combox 的页面里,后台做些处理。
public partial class CarrierWindow : ChildWindow
{
TermTypeView TermTypeViewModel = null;
JTCodeDictionaryView JTCodeDictionaryView = null;
public CarrierWindow()
{
InitializeComponent();
//设置数据源
initDictionaryData();
///
this.LayoutUpdated += new EventHandler(CarrierWindow_LayoutUpdated);
}
///
void CarrierWindow_LayoutUpdated(object sender, EventArgs e)
{
ComboBoxHelper.FixSelectedValueBindings(this);
}
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
{
this.MainTab.DataContext = this.DataSource;
}
}
- silverlight combox 绑定 source 时, 处理空值问题。
- silverlight combox selectvalue 值修改
- Silverlight 之 image source 绑定
- datagrid 绑定 combox 取值
- 处理Extjs3+ Combox默认值问题
- WPF combox数据绑定的问题
- WPF Combox selectedItem”不能绑定”的问题
- Silverlight 数据绑定 (2):Source to Target
- wpf combox 绑定值和设定值
- Silverlight之Combox用法
- Silverlight 数据绑定相关问题
- LigerUI中下拉框Combox级联操作中,后combox返回空串的问题
- Ext combox 取值问题
- GridView 使用 DropDownList 出现绑定空值的异常处理
- silverlight image绑定bitmap( Binding Image.Source from downloaded memory)
- DataGridview 动态绑定Combox
- DataGridview 动态绑定Combox
- combox绑定总结
- 推荐12款JavaScript表单插件
- CentOS 5.4 挂载Windows 分区FAT32/NTFS格式的方法
- android 从驱动到应用(一)
- 正交矩阵、正规矩阵和酉矩阵
- 简析社会化电子商务
- silverlight combox 绑定 source 时, 处理空值问题。
- CentOS–FTP安装与配置
- Oracle Sequence Cache 参数说明
- Larbin——一款c++编写的爬虫程序
- 右值引用
- Traceview的使用
- CentOS5.4 安装php5.2.x 和 ZendOptimizer-3.3.9的方法,方便支持phpMyAdmin-3.2.2
- mysql数据库无法插入和显示中文字符
- 使用Xmanager 远程连接 CentOS