【发布时间】:2014-07-26 10:49:45
【问题描述】:
如何注册基于数值类型的自定义属性?
public class NumberBox : TextBox
{
public static readonly DependencyProperty FormatProperty = DependencyProperty.Register("FormatValue", typeof(Type), typeof(NumberBox), new UIPropertyMetadata(default(Double)));
public Type FormatValue
{
get
{
return (Type)GetValue(FormatProperty);
}
set
{
SetValue(FormatProperty, value);
}
}
}
XAML
<nb:NumberBox FormatValue="{System:Int32}"/>
我敢肯定,这并不完美,但我真的不知道它是如何实现的。
更新:
基本上,我需要有一种方法来设置我的号码框的类型。例如,如果我需要使用Double NumberBox,我只需设置FormatValue="Double"
【问题讨论】:
-
为什么要使用类型作为属性?也许有更好的方法?
-
也许:) 但我不知道
-
为什么不使用蒙版文本框?
-
default(Double)以双精度返回0。您希望Type对象作为您的默认属性值 --typeof(Double)。 -
我同意@DanielA.White。你真正想解决什么问题?一旦你设置了 FormatValue 依赖属性,你打算用它做什么?如果我们知道最终结果,可能会有一个完全更好的解决方案,甚至可能不涉及额外的依赖属性。
标签: c# .net wpf dependency-properties