代码改变世界

silverlight中常用公共函数分享

2011-08-19 10:38  andy_zl  阅读(514)  评论(0)    收藏  举报

UserControl自动调整大小
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800"

//处理后台传过来的时间类型的空值(若后台传得时空值在异步交互过程中silverlight接受到的其实是一个默认值为0001-01-01的时间值)
public static string DateToString(DateTime dt, string format = "yyyy-MM-dd")
  {
     string str = dt.ToString(format);

     if (str.Contains("0001"))
        return "";
     else
        return str;
  }

        /// <summary>
        /// 设置radioButton 值
        /// </summary>
        /// <param name="list"></param>
        public static void setRadioButtion(List<RadioButton> list, string groupName, string value)
        {
            if (null != list && list.Count > 0)
            {
                foreach (RadioButton radioButton in list.Where(r => r.GroupName == groupName).ToList())
                {
                    if (!String.IsNullOrEmpty(value) && radioButton.Tag.ToString().Equals(value))
                    {
                        radioButton.IsChecked = true;
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// 得到radioButton 值
        /// </summary>
        /// <param name="list">radioButton List</param>
        /// <param name="groupName">radioButton groupName</param>
        /// <param name="ILogic">1返回tag 2返回content</param>
        /// <returns></returns>
        public static string getRadioButton(List<RadioButton> list, String groupName, int ILogic)
        {
            if (null != list && list.Count > 0)
            {
                foreach (RadioButton radioButton in list.Where(r => r.GroupName == groupName).ToList())
                {
                    if (radioButton.IsChecked == true && ILogic == 1)
                    {
                        return radioButton.Tag.ToString();
                    }
                    if (radioButton.IsChecked == true && ILogic == 2)
                    {
                        return radioButton.Content.ToString();
                    }

                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 设置checkBox控件是否被选中
        /// </summary>
        /// <param name="checkbox">控件名称</param>
        /// <param name="value">0不选中,1选中</param>
        public static void setCheckBox(CheckBox checkbox, String value)
        {
            int temp = (value == null || value.Trim().Equals("")) ? 0 : int.Parse(value);

            if (temp == 1)
                checkbox.IsChecked = true;
        }
        //设置值
        public static string getCheckBoxValue(CheckBox checkbox)
        {
            if (checkbox.IsChecked == true)
                return "1";
            else
                return "0";
        }
        //设置实体的radiobutton的值
        public static string returnRadioButtonValue(List<RadioButton> list, string groupName)
        {
            if (list != null)
            {
                foreach (RadioButton temp in list.Where(r => r.GroupName == groupName).ToList())
                {
                    if (temp.IsChecked == true)
                        return temp.Tag.ToString();
                }
            }
            return "0";
        }
        /// <summary>
        /// 设置实体的CheckBox的值
        /// </summary>
        /// <param name="data"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static bool setCheckBoxBySplit(string data, int index)
        {

            string[] datas = data.Split(',');
            try
            {
                if (datas[index] == "1")
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }
        //设置下拉列表的绑定项
        public static int getComboxSelectValue(ComboBox cmb, string value)
        {
            int flag = 0;
            int index = -1;
            if (value == null || value.Trim().Equals(""))
                return index;
            foreach (ComboBoxItem temp in cmb.Items)
            {
                index++;
                if (temp.Tag.ToString() == value)
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
                return -1;
            return index;
        }

        /// <summary>
        /// 获取子元素
        /// </summary>
        /// <param name="root"></param>
        /// <returns></returns>
        public static IEnumerable<DependencyObject> Descendents(DependencyObject root)
        {
            if (root.GetType() == typeof(TabControl))
            {
                TabControl tab = root as TabControl;
                TabItem tabItem = tab.Items[0] as TabItem;
                root = (DependencyObject)tabItem.Content;
            }
            int count = VisualTreeHelper.GetChildrenCount(root);
            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(root, i);
                yield return child;
                foreach (var descendent in Descendents(child))
                    yield return descendent;
            }
        }

        /// <summary>
        /// 检查combobox 选空返回NULL
        /// </summary>
        /// <param name="cmb">控件名称</param>
        /// <param name="ReturnValue">1为codeValue 2为Tag</param>
        /// <returns></returns>
        public static string CheckCombobox(ComboBox cmb, int ReturnValue)
        {
            if (cmb.SelectedItem != null && ReturnValue == 1)
            {
                return cmb.SelectedItem.GetPropertyValue("codeValue").toTrim();
            }
            if (cmb.SelectedItem != null && ReturnValue == 2)
            {
                return cmb.SelectedItem.GetPropertyValue("Tag").toTrim();
               
            }
            else
            {
                return null;
            }
        }