简体   繁体   English

参数为hibernateConnector的DataGridTextColumn ValidationRule不起作用

[英]DataGridTextColumn ValidationRule with parameter hibernateConnector not working

I try to implement a ValidationRule for a DataGridTextColumn that should test the nullability against a NHibernate property. 我尝试为DataGridTextColumn实施ValidationRule,该方法应针对NHibernate属性测试可空性。 The check is actually done within my HibernateConnector.isNullable(String className, String propertyName) method. 该检查实际上是在我的HibernateConnector.isNullable(String className, String propertyName)方法中完成的。 Therefore the HibernateConnector has to be passed to the ValidationRule. 因此,必须将HibernateConnector传递给ValidationRule。

The below referenced mynamespace.TeamsForm.xaml.cs has public HibernateConnector hibernateConnector { get; set; } 下面引用的mynamespace.TeamsForm.xaml.cs具有public HibernateConnector hibernateConnector { get; set; } public HibernateConnector hibernateConnector { get; set; }

Since I'm pretty new to WPF/XAML I implemented it using https://social.technet.microsoft.com/wiki/contents/articles/31422.wpf-passing-a-data-bound-value-to-a-validation-rule.aspx as an example. 由于我是WPF / XAML的新手,因此我使用https://social.technet.microsoft.com/wiki/contents/articles/31422.wpf-passing-a-data-bound-value-to-a-实现了它以validation-rule.aspx为例。

Wrapper class 包装类

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); }
    }
}

Binding proxy 绑定代理

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));
}

The validation class 验证类

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

The relevant XAML sections 相关的XAML部分

<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 -->

I can compile and run the code, but in EditRueckennummerValidationRule.Validate(...) at Wrapper.hibernateConnector.isNullable(...) hibernateConnector is null . 我可以编译和运行代码,但在EditRueckennummerValidationRule.Validate(...)Wrapper.hibernateConnector.isNullable(...) hibernateConnector为空

So what am I doing wrong here ? 那我在做什么错呢? Has it to do with the BindingProxy (all examples that I saw apparently have the same code) ? 与BindingProxy有关(我看到的所有示例显然都具有相同的代码)吗?

I needed a while to get understanding to "Data context" but think I meanwhile solved my own question: 我需要一段时间来理解“数据上下文”,但同时我想解决了自己的问题:

I moved 我搬家了

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

to

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

and added a line to 并添加了一行

public TeamsForm()
{
    InitializeComponent();
    DataContext = this; // <-- !!!! set the data context
}

This line I had missed from the example referenced above! 我从上面引用的示例中错过了这一行! Now Wrapper.hibernateConnector is set when it comes to validation. 现在,在验证时已设置Wrapper.hibernateConnector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM