silverlight中常用公共函数分享
UserControl自動(dòng)調(diào)整大小
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"
//處理后臺(tái)傳過(guò)來(lái)的時(shí)間類(lèi)型的空值(若后臺(tái)傳得時(shí)空值在異步交互過(guò)程中silverlight接受到的其實(shí)是一個(gè)默認(rèn)值為0001-01-01的時(shí)間值)
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>
??????? /// 設(shè)置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>
??????? /// 設(shè)置checkBox控件是否被選中
??????? /// </summary>
??????? /// <param name="checkbox">控件名稱(chēng)</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;
??????? }
??????? //設(shè)置值
??????? public static string getCheckBoxValue(CheckBox checkbox)
??????? {
??????????? if (checkbox.IsChecked == true)
??????????????? return "1";
??????????? else
??????????????? return "0";
??????? }
??????? //設(shè)置實(shí)體的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>
??????? /// 設(shè)置實(shí)體的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;
??????????? }
??????? }
??????? //設(shè)置下拉列表的綁定項(xiàng)
??????? 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">控件名稱(chēng)</param>
??????? /// <param name="ReturnValue">1為codeValue 2為T(mén)ag</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;
??????????? }
??????? }
轉(zhuǎn)載于:https://www.cnblogs.com/share-silverlight/archive/2011/08/19/2145396.html
總結(jié)
以上是生活随笔為你收集整理的silverlight中常用公共函数分享的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: C#分布式事务(TransactionS
- 下一篇: 函数-函数的基本组成
