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
{
///
/// 元素值抽象类
///
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; }
///
/// 关联控件
///
public FrameworkElement RefControl { get; private set; }
///
/// 获取控件值
///
///
public abstract object GetValue();
///
/// 设置控件值
///
///
public abstract void SetValue(object value);
///
/// 设置控件关联值
///
protected abstract Type GetControlType();
///
/// 判断是否是适配类型
///
///
///
public bool IsAdapterType(Type type)
{
return RefControlType == type;
}
public ElementValueProvider Clone()
{
return this.MemberwiseClone() as ElementValueProvider;
}
}
///
/// 文本框适配
///
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);
}
}
///
/// 下拉框适配
///
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);
}
}
///
/// 单选框适配
///
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);
}
}
///
/// 多选框适配
///
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);
}
}
///
/// 列表框框适配
///
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);
}
}
///
/// 文本框适配
///
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);
}
}
///
/// 文本框适配
///
public class WindowValueProvider : ElementValueProvider
{
public override object GetValue()
{
Window window = RefControl as Window;
string str = string.Empty;
if (window != null)
{
List valueList = new List();
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 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);
}
}
}