【发布时间】:2021-09-10 12:01:22
【问题描述】:
要求GridLength设置为最小值,但如果文本框中的数据增加(文本长度),则应将值从最小值设置为Auto。
//现在
public static readonly DependencyProperty BoxWidthProperty =
DependencyProperty.Register("BoxWidth", typeof(GridLength), typeof(ValueBox), new UIPropertyMetadata(new GridLength(80.0)));
// 类似的东西
public static readonly DependencyProperty BoxWidthProperty =
DependencyProperty.Register("BoxWidth", typeof(GridLength), typeof(ValueBox), new UIPropertyMetadata(new GridLength(80.0,GridUnitType.Auto)));
所以,我尝试将默认 GridLength 值设置为 80,但如果文本长度增加,它应该能够将值设置为 GridUnitType.Auto。
//字段属性
public GridLength BoxWidth
{
get
{
return (GridLength)GetValue(BoxWidthProperty);
}
set
{
SetValue(BoxWidthProperty, value);
}
}
【问题讨论】:
-
只使用网格长度是不可能的。最大和最小尺寸由单独的属性设置:MinHeight、MaxHeight。
-
@EldHasp ,谢谢你的意思。但是,我仍然认为我可能会添加一个额外的属性来设置框的宽度。该领域的任何帮助都更有意义。
-
如需更具体的答案,我们需要在您的问题中提供更多信息。您在哪个类中声明此属性?如果你完整地展示它的实现,那就更好了。你期望如何使用它,绑定它?
标签: c# wpf dependency-properties