【发布时间】:2010-08-26 17:45:52
【问题描述】:
我正在尝试将DataContext 中的属性绑定到ValidationRule 中的属性:
public class ReleaseValidationRule : ValidationRule
{
// I want to bind a value from my DataContext to this property:
public CheckboxViewModels ValidReleases { get; set; }
...
}
基于this thread,我创建了CheckboxViewModels 类只是为了充当List<CheckboxViewModel> 的包装器,以便列表可以是DependencyProperty,以便我可以绑定到它。但是,在我的ValidationRule 上的Validate 方法中,ValidReleases 列表始终为空。这是我的 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 属性(我绑定到CheckboxViewModels 的List 属性)有内容,因为我在TextBox 上方有一个TreeView,它显示了Releases 的内容.我在CheckboxViewModels.List 绑定上的转换器什么都不做,它只是我可以设置断点的地方。有趣的是,转换器断点永远不会被命中。就好像整行 <local:CheckboxViewModels List="{Binding Path=Releases, Converter={StaticResource debugConverter}}"/> 永远不会被执行,所以我的 ValidationRule 中的 ValidReleases 属性永远不会被设置。怎么回事?
编辑:这是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); }
}
}
【问题讨论】:
标签: c# wpf validation xaml dependency-properties