参数为hibernateConnector的DataGridTextColumn ValidationRule不起作用

时间:2018-07-20 13:29:08

标签: c# wpf validationrules datagridtextcolumn

我尝试为std::unordered_map实施ValidationRule,该规则应针对NHibernate属性测试可空性。 该检查实际上是在我的DataGridTextColumn方法中完成的。 因此,必须将HibernateConnector传递给ValidationRule。

下面引用的mynamespace.TeamsForm.xaml.cs具有HibernateConnector.isNullable(String className, String propertyName)

由于我是WPF / XAML的新手,所以我以https://social.technet.microsoft.com/wiki/contents/articles/31422.wpf-passing-a-data-bound-value-to-a-validation-rule.aspx为例来实现它。

包装器类

public HibernateConnector hibernateConnector { get; set; }

绑定代理

public class HibernateConnectionWrapper : DependencyObject
{
    public static readonly DependencyProperty HibernateConnectorProperty =
         DependencyProperty.Register("hibernateConnector", typeof(HibernateConnector),
         typeof(HibernateConnectionWrapper), new FrameworkPropertyMetadata(null));

    public HibernateConnector hibernateConnector
    {
        get { return (HibernateConnector)GetValue(HibernateConnectorProperty); }
        set { SetValue(HibernateConnectorProperty, value); }
    }
}

验证类

public class BindingProxy : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new PropertyMetadata(null));
}

相关的XAML部分

public class EditRueckennummerValidationRule : ValidationRule
{
    public HibernateConnectionWrapper Wrapper { get; set; }

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        bool n = Wrapper.hibernateConnector.isNullable(typeof(SpielerImTeam).FullName, "Rueckennummer");
        // ... more code

我可以编译并运行代码,但是在<rcappbase:AbstractWorkAreaForm x:Class="mynamespace.TeamsForm" ... <DataGrid.Resources> <BindingProxy Data="{Binding}" x:Key="proxy"/> </DataGrid.Resources> <!-- other elements --> <DataGridTextColumn Header="rueckennummer" IsReadOnly="False" DisplayIndex="5" > <DataGridTextColumn.Binding> <Binding Path="Rueckennummer" NotifyOnValidationError="True"> <Binding.ValidationRules> <local:EditRueckennummerValidationRule> <local:EditRueckennummerValidationRule.Wrapper> <mynamespaceandassembly:HibernateConnectionWrapper hibernateConnector="{Binding Path=Data.hibernateConnector, Source={StaticResource proxy}}"/> </local:EditRueckennummerValidationRule.Wrapper> </local:EditRueckennummerValidationRule> </Binding.ValidationRules> </Binding> </DataGridTextColumn.Binding> </DataGridTextColumn> <!-- more elements --> 的{​​{1}}中, hibernateConnector为空

那么我在这里做错了什么? 与BindingProxy有关(我看到的所有示例显然都具有相同的代码)吗?

1 个答案:

答案 0 :(得分:0)

我需要一段时间来理解“数据上下文”,但是我想同时解决了自己的问题:

我搬走了

nodemon.json

<DataGrid.Resources>
    <BindingProxy Data="{Binding}" x:Key="proxy"/>
</DataGrid.Resources>

并在其中添加一行

<rcappbase:AbstractWorkAreaForm.Resources>
    <BindingProxy Data="{Binding }" x:Key="proxy"/>
</rcappbase:AbstractWorkAreaForm.Resources>

我从上面引用的示例中错过了这一行! 现在public TeamsForm() { InitializeComponent(); DataContext = this; // <-- !!!! set the data context } 已设置为有效。