DataGridTextColumn ValidationRule with parameter hibernateConnector not working

1.4k Views Asked by At

I try to implement a ValidationRule for a DataGridTextColumn that should test the nullability against a NHibernate property. The check is actually done within my HibernateConnector.isNullable(String className, String propertyName) method. Therefore the HibernateConnector has to be passed to the ValidationRule.

The below referenced mynamespace.TeamsForm.xaml.cs has 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.

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

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

So what am I doing wrong here ? Has it to do with the BindingProxy (all examples that I saw apparently have the same code) ?

1

There are 1 best solutions below

0
user7399085 On

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.