- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我向名为 A 的 DependencyProperty 添加了一个 ValidateValueCallback。现在在验证回调中,应将 A 与名为 B 的 DependencyProperty 的值进行比较。但是如何在 static 中访问 B 的值ValidateValueCallback 方法 validateValue(对象值)?感谢您的任何提示!
示例代码:
class ValidateTest : DependencyObject
{
public static DependencyProperty AProperty = DependencyProperty.Register("A", typeof(double), typeof(ValidateTest), new PropertyMetadata(), validateValue);
public static DependencyProperty BProperty = DependencyProperty.Register("B", typeof(double), typeof(ValidateTest));
static bool validateValue(object value)
{
// Given value shall be greater than 0 and smaller than B - but how to access the value of B?
return (double)value > 0 && value <= /* how to access the value of B ? */
}
}
最佳答案
验证回调用于针对一组静态约束对给定输入值进行健全性检查。在您的验证回调中,检查正值是正确使用验证,但检查另一个属性则不是。如果需要确保给定值小于依赖属性,则应使用 property coercion ,像这样:
public static DependencyProperty AProperty = DependencyProperty.Register("A", typeof(double), typeof(ValidateTest), new PropertyMetadata(1.0, null, coerceValue), validateValue);
public static DependencyProperty BProperty = DependencyProperty.Register("B", typeof(double), typeof(ValidateTest), new PropertyMetaData(bChanged));
static object coerceValue(DependencyObject d, object value)
{
var bVal = (double)d.GetValue(BProperty);
if ((double)value > bVal)
return bVal;
return value;
}
static bool validateValue(object value)
{
return (double)value > 0;
}
虽然如果您设置 A > B(就像 ValidationCallback 所做的那样)这不会引发异常,但这实际上是所需的行为。由于您不知道设置属性的顺序,因此您应该支持以任何顺序设置属性。
如果 B 的值发生变化,我们还需要告诉 WPF 强制属性 A 的值,因为强制值可能会发生变化:
static void bChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
d.CoerceValue(AProperty);
}
关于c# - DependencyProperty ValidateValueCallback 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3055058/
我有一个包含只读 DependencyProperty 的类。从这个类中,我想绑定(bind)到只读 DependencyProperty到另一个只读 DepenendencyProperty另一个类
我有一个带有 ComboBox 的控件: 这是PropertyChangedCallback为属性(property) SelectedTrace在包含 ComboBox 的 OuterContro
我是 WPF/MVVM 的新手,我发现的示例似乎没有涵盖我面临的问题。 我有一个用于管理相当复杂的业务配置对象的屏幕。在 MVVM 中,我认为这意味着我应该具有以下内容: 具有接近于零逻辑的 XAML
我正在探索 WPF 世界,我在网上找到了一个关于如何在 xml 上使用绑定(bind)的好例子 http://www.codeproject.com/Articles/37854/How-to-Per
我有一个自定义类 MyPerson。所有(相关)属性都实现 INotifyPropertyChanged。 我创建了一个 UserControl 来显示它,一切正常。绑定(bind)到 MyPerso
我试图在我的代码中使用 tis 依赖属性,但它给了我错误,说默认值类型与属性“MyProperty”的类型不匹配。 但 short 应该接受 0 作为默认值。 如果我尝试给它一个 null 作为默认值
我在我的用户控件中定义了这样的属性: public bool IsSelected { get { return (bool)GetValue(IsSelectedProperty);
在我的用户控件中: public ODIF.DeviceChannel Channel { get { return (ODIF.DeviceChannel)GetValue(ChannelD
我在 WPF 应用程序的 ViewModel 中有这个简单的示例: class VM_DiskPartition : DependencyObject { // (...) Other
在previous post中我问如何将属性注册为 DependencyProperty。我得到了答案并且效果很好。 但现在我想在单击时向此 DependencyProperty 添加一些项目。这是行
我有两个对象绑定(bind)到同一个 dependencyProperty(在 Silverlight 中)。有没有办法确定这两个对象中的哪一个改变了属性?我想根据这些信息采取不同的行动。 不幸的是,
我正在将 XAML 序列化为文件并再次读取它(使用 XamlWriter 和 XamlReader)。 如果从未为元素设置 DependencyProperty(如 FrameworkElement
我有一个名为 ChartView 的用户控件。我有一个 ObservableCollection 类型的属性。我在 ChartView 中实现了 INotifyPropertyChanged。 Cha
虽然网络上的大多数代码示例都使用 DependencyProperties 的静态声明,但我发现在某些情况下它们被定义为公共(public)只读实例成员。 将 DependencyProperty 定
我有析构函数问题。这是重现问题的代码: class DPDemo : DependencyObject { public DPDemo() { } ~DPDemo()
在我的 XAML 中,我有一个带有 DependencyProperty 的对象: 我还有MouseUp处理程序 TextBlock .我如何获得 OverWidth属性值在里面? 最佳答案 这似乎
我正在创建一个模仿 AppBarButton 的自定义控件(但具有自定义功能,因此我们无法从 AppBarButton 派生)。 我的问题是 AppBarButton 的 Icon 属性。该属性本身采
如何将属性名称(字符串形式)转换为 DependencyProperty? 我有一组属性名称、它的字符串值和一个 DependencyObject。现在我想将这些属性值设置为 DependencyOb
我的控件有一个映射到私有(private)变量的属性。设置属性时,我还需要存储某个其他变量。当属性的私有(private)变量由我自己的控制代码设置时,一定不会出现这种特殊处理。一切顺利。 我现在需要
我正在尝试创建一个用户控件,其中包含一些 DependencyProperties,这些属性会转发给用户控件中的子控件。经过几次尝试,我开始工作了。为了测试一个小例子。 在示例中,我有一个名为 Ctr
我是一名优秀的程序员,十分优秀!