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) ?
I needed a while to get understanding to "Data context" but think I meanwhile solved my own question:
I moved
to
and added a line to
This line I had missed from the example referenced above! Now
Wrapper.hibernateConnectoris set when it comes to validation.