zoukankan      html  css  js  c++  java
  • Silverlight: 为ComboBox实现SelectedValue属性

    silverlight中的comboBox并不像一般asp.net的dropdownlist或winform中的comboBox,有selectedValue属性。因为在silverlight中使用comboBox使用selectedItem就基本上满足了需求,如果还不够可以再为相应Tag赋值就OK了。

    下面主要说一下为comboBox扩展一个selectedValue属性,其实也很简单,只要为ComboBox注册一个就OK。

    1.  一个简单的效果图:

    2.  调用方法:

    代码
    ComboBoxItemValueExtend cbmValueExtend = new ComboBoxItemValueExtend();
    cbmValueExtend.SelectionChanged
    += (sender, arg) =>
    {
    UI.Alert(cbmValueExtend.SelectedValue);
    };
    cbmValueExtend.ItemsSource
    = DepartmentDataSource();
    cbmValueExtend.DisplayMemberPath
    = "Name";
    cbmValueExtend.SelectedValuePath
    = "ID";

    Dialog dlg
    = new Dialog()
    {
    Height
    = 300,
    Width
    = 400,
    Title
    = "ComboBox's SelectedValue Sample",
    Content
    = new AutoGrid(3, 60, 180)
    {
    Children
    =
    {
    new Label(){Content = "请选择:"},
    cbmValueExtend
    }
    }
    };
    dlg.Show();

    3.  ComboBoxItemValueExtend的实现代码

    代码
    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;
    using System.Linq;

    namespace Top.Windows.Settings
    {
    public class ComboBoxItemValueExtend : ComboBox
    {
    #region DependencyProperty: SelectedValuePathProperty-----------------------------------------
    public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(ComboBoxItemValueExtend), null);

    /// <summary>
    /// Set the name or path of the property that is value for each date item.
    /// </summary>
    public string SelectedValuePath
    {
    get
    {
    return (string)GetValue(ComboBoxItemValueExtend.SelectedValuePathProperty);
    }
    set
    {
    SetValue(ComboBoxItemValueExtend.SelectedValuePathProperty, value);
    }
    }
    #endregion DependencyProperty: SelectedValuePathProperty-----------------------------------------


    #region DependencyProperty: SelectedValueProperty-------------------------------------
    public static DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(ComboBoxItemValueExtend), new PropertyMetadata(new PropertyChangedCallback(SelectedValuePropertyChanged)));

    /// <summary>
    /// Get Selected Value in the comboBox's SelectedItme
    /// </summary>
    public object SelectedValue
    {
    get
    {
    return GetValue(SelectedValueProperty);
    }
    set
    {
    try
    {
    var q
    = (from item in Items
    where item.GetType().GetProperty(SelectedValuePath).GetValue(item, null).Equals(value)
    select item).Single();
    SelectedItem
    = q;
    }
    catch
    {
    throw new Exception();
    }
    }
    }

    private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    (d
    as ComboBoxItemValueExtend).SelectedValue = e.NewValue;
    }
    #endregion DependencyProperty: SelectedValueProperty-------------------------------------


    public ComboBoxItemValueExtend()
    :
    base()
    {
    base.SelectionChanged += new SelectionChangedEventHandler(ComboBoxItemValueExtend_SelectionChanged);
    }

    void ComboBoxItemValueExtend_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    if (SelectedItem != null && !string.IsNullOrEmpty(SelectedValuePath))
    {
    try
    {
    SetValue(ComboBoxItemValueExtend.SelectedValueProperty, SelectedItem.GetType().GetProperty(SelectedValuePath).GetValue(SelectedItem,
    null));
    var value
    = SelectedValue;
    }
    catch
    {
    throw new Exception();
    }
    }
    }
    }
    }

    4.  DepartmentDataSource()方法是获取一些测试数据

    代码
    public struct Department
    {
    public int ID { set; get; }
    public int ParentId { set; get; }
    public string Name { set; get; }
    public string Description { set; get; }
    }

    public List<Department> DepartmentDataSource()
    {
    List
    <Department> list = new List<Department>() {
    new Department(){ID = 1, ParentId = 0, Name = "Namespace"},
    new Department(){ID = 2, ParentId = 1, Name = "Class"},
    new Department(){ID = 3, ParentId = 1, Name = "Enum"},
    new Department(){ID = 4, ParentId = 2, Name = "Method"},
    new Department(){ID = 5, ParentId = 4, Name = "Attribute"},
    new Department(){ID = 6, ParentId = 5, Name = "Private"},
    new Department(){ID = 7, ParentId = 5, Name = "Publict"},
    new Department(){ID = 8, ParentId = 7, Name = "DepartmentDataSource()"},
    };
    return list;
    }

    谢谢指导!

    备注:

    关于DependencyProperty的相关知道可以翻阅MSDN学习一下,里面有详细的介绍,这里不再复述!

    http://msdn.microsoft.com/zh-cn/library/cc221408.aspx

  • 相关阅读:
    hdu1421 搬寝室(dp)
    HDU 2577(DP)
    扩展欧几里德算法
    unique函数的作用
    区间更新 zoj3911
    set的应用
    vue 事件处理器
    vue Class与style绑定
    vue的计算属性
    sass入门
  • 原文地址:https://www.cnblogs.com/blackcore/p/1717655.html
Copyright © 2011-2022 走看看