C
C#2y ago
Anton

WPF revalidate the object on any property change [Answered]

<DataGridTemplateColumn Header="Has Owner">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox
local:Properties.Focus="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsChecked="{Binding HasOwner, Mode=TwoWay, ValidatesOnDataErrors=True}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

<DataGridTemplateColumn Header="Owner First Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox
local:Properties.Focus="True"
Text="{Binding Owner_FirstName, Mode=TwoWay, ValidatesOnDataErrors=True}"
IsEnabled="{Binding HasOwner}"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Has Owner">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox
local:Properties.Focus="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsChecked="{Binding HasOwner, Mode=TwoWay, ValidatesOnDataErrors=True}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

<DataGridTemplateColumn Header="Owner First Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox
local:Properties.Focus="True"
Text="{Binding Owner_FirstName, Mode=TwoWay, ValidatesOnDataErrors=True}"
IsEnabled="{Binding HasOwner}"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
8
2 Replies
Anton
Anton2y ago
Say, one of my properties depends on another property. For example, the first name should only be validated if we have an owner, and once the owner is toggled, the first name should be revalidated, to show errors if there are any. How do I acheieve that? I'm not even asking about creating a graph of what property depends on what other property, what I want is simply a way to revalidate the whole data source once anything changes Right now I'm using IDataErrorInfo for validation, but the indexer is not called when a property changes I made a custom extension, feels ok
[MarkupExtensionReturnType(typeof(Binding))]
public class ReactiveBinding : MarkupExtension
{
public string Path { get; set; }
public string StringFormat { get; set; }

public ReactiveBinding(string path)
{
Path = path;

}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return new Binding(Path)
{
Mode = BindingMode.TwoWay,
ValidatesOnDataErrors = true,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
StringFormat = StringFormat,
};
}
}
[MarkupExtensionReturnType(typeof(Binding))]
public class ReactiveBinding : MarkupExtension
{
public string Path { get; set; }
public string StringFormat { get; set; }

public ReactiveBinding(string path)
{
Path = path;

}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return new Binding(Path)
{
Mode = BindingMode.TwoWay,
ValidatesOnDataErrors = true,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
StringFormat = StringFormat,
};
}
}
Of course, for it to be 100% compatible, you'd need to mirror all of the properties You can't inherit from the binding one, can you?
Accord
Accord2y ago
✅ This post has been marked as answered!