自定义控件数据绑定和随窗体自动缩放大小

大C小d 2019-06-19 05:32:24
样式图如下(OK图层下面还有一个BAD图层):


1.自定义控件代码,这里控制包含控件的自动缩放参照(https://blog.csdn.net/wwwxrlbjcom/article/details/80568041

public partial class ucResultItem : UserControl
{
public ucResultItem()
{
InitializeComponent();
}

[Category("自定义属性"), Description("GroupText")]
public string GroupText
{
get { return this.groupBox.Text; }
set { this.groupBox.Text = value; }
}

[Category("自定义属性"), Description("DisPrecision")]
public int DisPrecision
{
get => disPrecision;

set
{
if(value>0 && value <=5)
{
disPrecision = value;
precisionStr = "0.";
precisionStr= precisionStr.PadRight(disPrecision+2, '#');
}

}
}

private int disPrecision = 3;
private string precisionStr="0.###";


public void Binding(ResultItem result)
{
lb_Value.DataBindings.Add("Text", result, GetPropName<ResultItem>(rst => result.Value));
cb_Enable.DataBindings.Add("Checked", result, GetPropName<ResultItem>(rst => result.CmpEnable));
tb_UpValue.DataBindings.Add("Text", result, GetPropName<ResultItem>(rst => result.UpValue));
tb_LowValue.DataBindings.Add("Text", result, GetPropName<ResultItem>(rst => result.LowValue));
lb_OK.DataBindings.Add("Visible", result, GetPropName<ResultItem>(rst => result.Result));

//此处DataBindings里面的result可以用接口实现
}
string GetPropName<T>(Expression<Func<T, object>> expr)
{
switch (expr.Body.NodeType)
{
case ExpressionType.MemberAccess:
return ((MemberExpression)expr.Body).Member.Name;
case ExpressionType.Convert:
return ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
default:
return null;
}
}


private void lb_Value_TextChanged(object sender, EventArgs e)
{
lb_Value.Text = Convert.ToDouble(lb_Value.Text).ToString(precisionStr);
}

private float x=140;
private float y =160;

/// <summary>
/// 將控制項的寬,高,左邊距,頂邊距和字體大小暫存到tag屬性中
/// </summary>
/// <param name="cons">遞歸控制項中的控制項</param>
private void SetTag(Control cons)
{
foreach (Control con in cons.Controls)
{
con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
if (con.Controls.Count > 0)
SetTag(con);
}
}
private void SetControls(float newx, float newy, Control cons)
{
//遍歷窗體中的控制項,重新設置控制項的值
foreach (Control con in cons.Controls)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });//獲取控制項的Tag屬性值,並分割後存儲字元串數組
float a = System.Convert.ToSingle(mytag[0]) * newx;//根據窗體縮放比例確定控制項的值,寬度
con.Width = (int)a;//寬度
a = System.Convert.ToSingle(mytag[1]) * newy;//高度
con.Height = (int)(a);
a = System.Convert.ToSingle(mytag[2]) * newx;//左邊距離
con.Left = (int)(a);
a = System.Convert.ToSingle(mytag[3]) * newy;//上邊緣距離
con.Top = (int)(a);
Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字體大小
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
SetControls(newx, newy, con);
}
}
}


private void ucResultItem_Resize(object sender, EventArgs e)
{
float newx = (this.Width) / x; //窗體寬度縮放比例
float newy = (this.Height) / y;//窗體高度縮放比例
SetControls(newx, newy, this);//隨窗體改變控制項大小
}

/// <summary>
/// 固定纵横比
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ucResultItem_SizeChanged(object sender, EventArgs e)
{
this.Width = this.Height * 7 / 8;
}

private void ucResultItem_Load(object sender, EventArgs e)
{
this.Resize += new EventHandler(ucResultItem_Resize);
this.SizeChanged += new EventHandler(ucResultItem_SizeChanged);
SetTag(this);//調用方法
ucResultItem_Resize(new object(), new EventArgs());
}

}

2.定义需要绑定的数据实体类,实体重新赋值时需要触发属性变化

[Serializable]
public class ResultItem : BaseResult
{
private double value = default(double);
private double upValue = default(double);
private double lowValue = default(double);
private bool result = default(bool);
private bool cmpEnable = default(bool);


public double Value
{
get => value;
set
{
if (this.value != value)
{
this.value = value;
OnPropertyChanged("Value");
}
}
}


public double UpValue
{
get => upValue;
set
{
if (upValue != value)
{
upValue = value;
OnPropertyChanged("UpValue");
}
}
}

public double LowValue
{
get => lowValue;
set
{
if (lowValue != value)
{
lowValue = value;
OnPropertyChanged("LowValue");
}
}
}

public bool Result
{
get => result;
set
{
if (result != value)
{
result = value;

//resultStr = result ? "OK" : "NG";

OnPropertyChanged("Result");
//OnPropertyChanged("ResultStr");
}
}
}

public bool CmpEnable
{
get => cmpEnable;
set
{
if (cmpEnable != value)
{
cmpEnable = value;
OnPropertyChanged("CmpEnable");
}
}
}

public void ComputedResult()
{
if (CmpEnable)
{
Result = (LowValue <= Value) && (Value <= UpValue);
}
else
{
Result = true;
}
}
}

public class BaseResult : INotifyPropertyChanged
{
#region INotifyPropertyChanged 成员

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string propertyName)
{
if (propertyName == null)
{
throw new ArgumentNullException("propertyName");
}

var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}

3.ui调用

ucResultItem1.Binding(CurrentInfo.result1);
...全文
118 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
大C小d 2019-06-20
  • 打赏
  • 举报
回复
第一次发帖不知道发哪儿,这个问题已经解决了,只是把开发的内容发布出来,供以后参考
qq14923349 2019-06-19
  • 打赏
  • 举报
回复
?所以你的问题是?

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧