123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using FWindSoft.Data;
- using FWindSoft.SystemExtensions;
- namespace FWindSoft.Wpf.History
- {
- /// <summary>
- /// 元素值抽象类
- /// </summary>
- public abstract class ElementValueProvider:IValueProvider
- { /*
- 为统一接口,不把相关control 参数放在 GetValue和SetValue方法中
- */
- public ElementValueProvider()
- {
- RefControlType=GetControlType();
- }
- public void SetAdapterControl(FrameworkElement element)
- {
- RefControl = element;
- }
- public Type RefControlType { get; private set; }
- /// <summary>
- /// 关联控件
- /// </summary>
- public FrameworkElement RefControl { get; private set; }
- /// <summary>
- /// 获取控件值
- /// </summary>
- /// <returns></returns>
- public abstract object GetValue();
- /// <summary>
- /// 设置控件值
- /// </summary>
- /// <param name="value"></param>
- public abstract void SetValue(object value);
- /// <summary>
- /// 设置控件关联值
- /// </summary>
- protected abstract Type GetControlType();
- /// <summary>
- /// 判断是否是适配类型
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public bool IsAdapterType(Type type)
- {
- return RefControlType == type;
- }
- public ElementValueProvider Clone()
- {
- return this.MemberwiseClone() as ElementValueProvider;
- }
- }
- /// <summary>
- /// 文本框适配
- /// </summary>
- public class TextBoxValueProvider : ElementValueProvider
- {
- public override object GetValue()
- {
- TextBox textBox = RefControl as TextBox;
- string str = string.Empty;
- if (textBox != null)
- {
- str = textBox.Text;
- }
- return str;
- }
- public override void SetValue(object value)
- {
- TextBox textBox = RefControl as TextBox;
- object str = value??string.Empty;
- if (textBox != null)
- {
- textBox.Text= str.ToString();
- }
- }
- protected override Type GetControlType()
- {
- return typeof(TextBox);
- }
- }
- /// <summary>
- /// 下拉框适配
- /// </summary>
- public class ComBoxValueProvider : ElementValueProvider
- {
- public override object GetValue()
- {
- ComboBox cmb = RefControl as ComboBox;
- string str = string.Empty;
- if (cmb != null)
- {
- str = cmb.Text;
- }
- return str;
- }
- public override void SetValue(object value)
- {
- ComboBox cmb = RefControl as ComboBox;
- object str = value ?? string.Empty;
- if (cmb != null)
- {
- cmb.Text = str.ToString();
- if (cmb.SelectedIndex == -1 && cmb.Items.Count > 0 && !cmb.IsEditable)
- {
- cmb.SelectedIndex = 0;
- }
- }
- }
- protected override Type GetControlType()
- {
- return typeof(ComboBox);
- }
- }
- /// <summary>
- /// 单选框适配
- /// </summary>
- public class RadioValueProvider : ElementValueProvider
- {
- public override object GetValue()
- {
- RadioButton radioButton = RefControl as RadioButton;
- string str = string.Empty;
- if (radioButton != null)
- {
- str = radioButton.IsChecked.ToString();
- }
- return str;
- }
- public override void SetValue(object value)
- {
- RadioButton radioButton = RefControl as RadioButton;
- object str = value ?? string.Empty;
- if (radioButton != null)
- {
- bool isCheck =str.ToString().ToBoolean();
- if (isCheck) //如果是false则不给相应radio赋值,避免不必要的界面交互造成的bug
- radioButton.IsChecked = isCheck;
- }
- }
- protected override Type GetControlType()
- {
- return typeof(RadioButton);
- }
- }
- /// <summary>
- /// 多选框适配
- /// </summary>
- public class CheckBoxValueProvider : ElementValueProvider
- {
- public override object GetValue()
- {
- CheckBox checkBox = RefControl as CheckBox;
- string str = string.Empty;
- if (checkBox != null)
- {
- str = checkBox.IsChecked.ToString();
- }
- return str;
- }
- public override void SetValue(object value)
- {
- CheckBox checkBox = RefControl as CheckBox;
- object str = value ?? string.Empty;
- if (checkBox != null)
- {
- checkBox.IsChecked = str.ToString().ToBoolean();
- }
- }
- protected override Type GetControlType()
- {
- return typeof(CheckBox);
- }
- }
- /// <summary>
- /// 列表框框适配
- /// </summary>
- public class ListBoxValueProvider : ElementValueProvider
- {
- public override object GetValue()
- {
- ListBox listBox = RefControl as ListBox;
- string str ="0";
- if (listBox != null)
- {
- str = listBox.SelectedIndex.ToString();
- }
- return str;
- }
- public override void SetValue(object value)
- {
- ListBox listBox = RefControl as ListBox;
- object str = value ?? string.Empty;
- if (listBox != null)
- {
- int index = 0;
- if (int.TryParse(str.ToString(), out index))
- {
- if (listBox.Items.Count > index)
- {
- listBox.SelectedIndex = index;
- }
- }
- }
- }
- protected override Type GetControlType()
- {
- return typeof(ListBox);
- }
- }
- /// <summary>
- /// 文本框适配
- /// </summary>
- public class TextBlockValueProvider : ElementValueProvider
- {
- public override object GetValue()
- {
- TextBlock textBox = RefControl as TextBlock;
- string str = string.Empty;
- if (textBox != null)
- {
- str = textBox.Text;
- }
- return str;
- }
- public override void SetValue(object value)
- {
- TextBlock textBox = RefControl as TextBlock;
- object str = value ?? string.Empty;
- if (textBox != null)
- {
- textBox.Text = str.ToString();
- }
- }
- protected override Type GetControlType()
- {
- return typeof(TextBlock);
- }
- }
- /// <summary>
- /// 文本框适配
- /// </summary>
- public class WindowValueProvider : ElementValueProvider
- {
- public override object GetValue()
- {
- Window window = RefControl as Window;
- string str = string.Empty;
- if (window != null)
- {
- List<string> valueList = new List<string>();
- valueList.Add(window.Top.ToString());
- valueList.Add(window.Left.ToString());
- valueList.Add(window.Width.ToString());
- valueList.Add(window.Height.ToString());
- str = string.Join(",", valueList);
- }
- return str;
- }
- public override void SetValue(object value)
- {
- Window window = RefControl as Window;
- string str = value?.ToString()?? string.Empty;
- if (string.IsNullOrEmpty(str)|| window != null)
- {
- List<string> valueList = str.Split(',').ToList();
- try
- {
- if (valueList.Count > 3)
- {
- window.Top = valueList[0].ToDouble();
- window.Left = valueList[1].ToDouble();
- window.Width = valueList[2].ToDouble();
- window.Height = valueList[3].ToDouble();
- }
- }
- catch (Exception)
- {
- System.Diagnostics.Debug.WriteLine("窗口赋值出错");
- }
-
- }
- }
- protected override Type GetControlType()
- {
- return typeof(Window);
- }
- }
- }
|