Reputation: 559
Given the following View Model example
public class MyViewModel
{
public ObservableCollection<MyObjType> BoundItems { get; }
}
and MyObjType
public class MyObjType
{
public string Name { get; set; }
public int Id { get; set; }
}
I have added a Validation rule to a DataGrid Column, where the DataGrid is bound to the BoundItems
collection in my ViewModel, and the Text property in the Template Column is bound to the Name.
<DataGrid ItemsSource="{Binding BoundItems}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TexBox>
<TextBox.Text>
<Binding Path="Name" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<xns:MyValidationRule>
<xns:MyValidationRule.SomeDependencyProp>
<xns:SomeDependencyProp SubProp={Binding Id} /> <!-- Not Working -->
</xns:MyValidationRule.SomeDependencyProp>
</xns:MyValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
...
</DataGrid.Columns>
</DataGrid>
I want to pass another property Id
of my collection type (MyObjType
) to the validation rule, how do I access that from the rule. I know about the freezable and getting the context of the view model, but i need another property of my collection type that is bound to the Datagrid.
The ValidationRule and SomeDependencyProp is modeled after the example here: https://social.technet.microsoft.com/wiki/contents/articles/31422.wpf-passing-a-data-bound-value-to-a-validation-rule.aspx
public class SomeDependencyProp : DependencyObject
{
public static readonly SubPropProperty =
DependencyProperty.Register("SubProp", typeof(int),
typeof(SomeDependencyProp), new FrameworkPropertyMetadata(0));
public int SubProp{
get { return (int)GetValue(SubPropProperty ); }
set { SetValue(SubPropProperty, value); }
}
}
public class MyValidationRule: System.Windows.Controls.ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
...
}
public SomeDependencyProp SomeDependencyProp { get; set; }
}
Upvotes: 0
Views: 264
Reputation: 37059
The solution to this situation is to use a BindingProxy.
Upvotes: 1