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










share|improve this question






















  • 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















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










share|improve this question






















  • 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













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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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
















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]