How to implement WPF Binding Validation in C# code
up vote
1
down vote
favorite
I am trying to generalize the sample provided by Microsoft illustrating how to implement a Binding Validation [https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-binding-validation#see-also] by reformulating it all in C#.
(1) I am trying to convert the following XAML binding to C#:
<TextBox Name="textBox1" Width="50" FontSize="15"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}"
Grid.Row="1" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
(2) and the following XAML which creates a red exclamation mark to notify the user of a validation error to C#:
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
(3) also the XAML which creates a ToolTip to C# that shows the error message is created:
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
My goal is to make it reusable in many places by defining a class which can construct/define both the text field and the binding in my program to connect text fields to integer or double type properties. Although similar question was asked before [WPF C# how to create THIS binding in code? in this case XAML code for defining the binding and the helper utilities/methods are fairly complex.
Any comment, suggestion, or help would be appreciated.
i.konuk
c# wpf xaml
add a comment |
up vote
1
down vote
favorite
I am trying to generalize the sample provided by Microsoft illustrating how to implement a Binding Validation [https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-binding-validation#see-also] by reformulating it all in C#.
(1) I am trying to convert the following XAML binding to C#:
<TextBox Name="textBox1" Width="50" FontSize="15"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}"
Grid.Row="1" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
(2) and the following XAML which creates a red exclamation mark to notify the user of a validation error to C#:
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
(3) also the XAML which creates a ToolTip to C# that shows the error message is created:
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
My goal is to make it reusable in many places by defining a class which can construct/define both the text field and the binding in my program to connect text fields to integer or double type properties. Although similar question was asked before [WPF C# how to create THIS binding in code? in this case XAML code for defining the binding and the helper utilities/methods are fairly complex.
Any comment, suggestion, or help would be appreciated.
i.konuk
c# wpf xaml
UserControl is way to go.
– RajN
Nov 18 at 2:49
1
You are already on the right track by using aValidationRule
- don't try and convert this all to C# because you don't need to. Define a global style that you can apply to the TextBox and stick to the bound validation rule. Check this blog post by me to see if it helps.
– slugster
Nov 18 at 4:30
slugster, thank you indeed very much for the suggestion and encouragement. I should have thought of style! I will try and post update if I make progress.
– I. Konuk
Nov 18 at 13:41
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to generalize the sample provided by Microsoft illustrating how to implement a Binding Validation [https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-binding-validation#see-also] by reformulating it all in C#.
(1) I am trying to convert the following XAML binding to C#:
<TextBox Name="textBox1" Width="50" FontSize="15"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}"
Grid.Row="1" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
(2) and the following XAML which creates a red exclamation mark to notify the user of a validation error to C#:
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
(3) also the XAML which creates a ToolTip to C# that shows the error message is created:
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
My goal is to make it reusable in many places by defining a class which can construct/define both the text field and the binding in my program to connect text fields to integer or double type properties. Although similar question was asked before [WPF C# how to create THIS binding in code? in this case XAML code for defining the binding and the helper utilities/methods are fairly complex.
Any comment, suggestion, or help would be appreciated.
i.konuk
c# wpf xaml
I am trying to generalize the sample provided by Microsoft illustrating how to implement a Binding Validation [https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-binding-validation#see-also] by reformulating it all in C#.
(1) I am trying to convert the following XAML binding to C#:
<TextBox Name="textBox1" Width="50" FontSize="15"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}"
Grid.Row="1" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
(2) and the following XAML which creates a red exclamation mark to notify the user of a validation error to C#:
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
(3) also the XAML which creates a ToolTip to C# that shows the error message is created:
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
My goal is to make it reusable in many places by defining a class which can construct/define both the text field and the binding in my program to connect text fields to integer or double type properties. Although similar question was asked before [WPF C# how to create THIS binding in code? in this case XAML code for defining the binding and the helper utilities/methods are fairly complex.
Any comment, suggestion, or help would be appreciated.
i.konuk
c# wpf xaml
c# wpf xaml
asked Nov 18 at 2:19
I. Konuk
62
62
UserControl is way to go.
– RajN
Nov 18 at 2:49
1
You are already on the right track by using aValidationRule
- don't try and convert this all to C# because you don't need to. Define a global style that you can apply to the TextBox and stick to the bound validation rule. Check this blog post by me to see if it helps.
– slugster
Nov 18 at 4:30
slugster, thank you indeed very much for the suggestion and encouragement. I should have thought of style! I will try and post update if I make progress.
– I. Konuk
Nov 18 at 13:41
add a comment |
UserControl is way to go.
– RajN
Nov 18 at 2:49
1
You are already on the right track by using aValidationRule
- don't try and convert this all to C# because you don't need to. Define a global style that you can apply to the TextBox and stick to the bound validation rule. Check this blog post by me to see if it helps.
– slugster
Nov 18 at 4:30
slugster, thank you indeed very much for the suggestion and encouragement. I should have thought of style! I will try and post update if I make progress.
– I. Konuk
Nov 18 at 13:41
UserControl is way to go.
– RajN
Nov 18 at 2:49
UserControl is way to go.
– RajN
Nov 18 at 2:49
1
1
You are already on the right track by using a
ValidationRule
- don't try and convert this all to C# because you don't need to. Define a global style that you can apply to the TextBox and stick to the bound validation rule. Check this blog post by me to see if it helps.– slugster
Nov 18 at 4:30
You are already on the right track by using a
ValidationRule
- don't try and convert this all to C# because you don't need to. Define a global style that you can apply to the TextBox and stick to the bound validation rule. Check this blog post by me to see if it helps.– slugster
Nov 18 at 4:30
slugster, thank you indeed very much for the suggestion and encouragement. I should have thought of style! I will try and post update if I make progress.
– I. Konuk
Nov 18 at 13:41
slugster, thank you indeed very much for the suggestion and encouragement. I should have thought of style! I will try and post update if I make progress.
– I. Konuk
Nov 18 at 13:41
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357336%2fhow-to-implement-wpf-binding-validation-in-c-sharp-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
UserControl is way to go.
– RajN
Nov 18 at 2:49
1
You are already on the right track by using a
ValidationRule
- don't try and convert this all to C# because you don't need to. Define a global style that you can apply to the TextBox and stick to the bound validation rule. Check this blog post by me to see if it helps.– slugster
Nov 18 at 4:30
slugster, thank you indeed very much for the suggestion and encouragement. I should have thought of style! I will try and post update if I make progress.
– I. Konuk
Nov 18 at 13:41