WPF,ValidationRule中的属性从未设置 [英] WPF, property in ValidationRule never set
问题描述
我正在尝试将 DataContext
中的属性绑定到 ValidationRule
中的属性:
I'm trying to bind a property in my DataContext
to a property in a ValidationRule
:
public class ReleaseValidationRule : ValidationRule
{
// I want to bind a value from my DataContext to this property:
public CheckboxViewModels ValidReleases { get; set; }
...
}
基于此线程,我创建了 CheckboxViewModels
类只是用作 List< CheckboxViewModel>
的包装,因此列表可以是 DependencyProperty
,以便我可以绑定到它。但是,在我的 ValidationRule
的 Validate
方法中, ValidReleases
列表始终为空。这是我的XAML:
Based on this thread, I created the CheckboxViewModels
class just to act as a wrapper for a List<CheckboxViewModel>
so that the list could be a DependencyProperty
so that I could bind to it. However, in my Validate
method on my ValidationRule
, the ValidReleases
list is always empty. Here's my XAML:
<TextBox>
<TextBox.Text>
<Binding Path="Release" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:ReleaseValidationRule>
<local:ReleaseValidationRule.ValidReleases>
<local:CheckboxViewModels List="{Binding Path=Releases,
Converter={StaticResource debugConverter}}"/>
</local:ReleaseValidationRule.ValidReleases>
</local:ReleaseValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
我知道 Releases
属性(我知道'm绑定到 CheckboxViewModels
的 List
属性的内容具有内容,因为我有一个 TreeView
在 TextBox
上方,该文本框显示 Releases
的内容。我在 CheckboxViewModels.List
绑定中拥有的转换器没有任何作用,它只是我可以设置断点的地方。有趣的是,转换器断点永远不会被击中。好像整行< local:CheckboxViewModels List = {Binding Path = Releases,Converter = {StaticResource debugConverter}} />
从未执行过,所以我的 ValidationRule
中的 ValidReleases
属性从未设置。
I know the Releases
property (what I'm binding to the List
property of CheckboxViewModels
) has content because I have a TreeView
just above the TextBox
that shows the contents of Releases
. The converter I have on the CheckboxViewModels.List
binding does nothing, it's just a place where I can set a breakpoint. The funny thing is, that converter breakpoint never gets hit. It's as if the whole line <local:CheckboxViewModels List="{Binding Path=Releases, Converter={StaticResource debugConverter}}"/>
never gets executed, so the ValidReleases
property in my ValidationRule
never gets set. What's going on?
编辑:这是 CheckboxViewModels
的样子:
public class CheckboxViewModels : DependencyObject, IList<CheckboxViewModel>,
IEnumerable<CheckboxViewModel>
{
...members necessary to implement IList, IEnumerable...
public static readonly DependencyProperty ListProperty =
DependencyProperty.Register(
"List",
typeof(List<CheckboxViewModel>),
typeof(CheckboxViewModels),
new PropertyMetadata(new List<CheckboxViewModel>())
);
public List<CheckboxViewModel> List
{
get { return (List<CheckboxViewModel>)GetValue(ListProperty); }
set { SetValue(ListProperty, value); }
}
}
推荐答案
我停止使用验证规则,而是遵循关于实现 IDataErrorInfo
的教程。那是我进入 this [string]
找出错误消息时的类实例已完全初始化。我一直试图传递的数据只是来自同一实例上另一个属性的数据。也就是说,我想使用来自 Property2
的一些数据来验证 Property1
。使用 IDataErrorInfo
,当我验证 Property1
并在必要时组装错误消息时,我可以访问 Property2
,而无需传入任何内容。
I stopped using validation rules and instead followed this tutorial about implementing IDataErrorInfo
. That was my class instance is fully initialized by the time I get into this[string]
to figure out the error message. The data I had been trying to pass in was just data from another property on the same instance. That is, I wanted to validate Property1
using some data from Property2
. With IDataErrorInfo
, when I'm validating Property1
and assembling an error message if necessary, I have access to Property2
as desired, without having to pass anything in.
这篇关于WPF,ValidationRule中的属性从未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!