Issue with binding and ValidationRules
-
I have a problem with ValidationRules. Basically the data binding works, but the Validate-method of the ValidationRules never fire. This is part of my XAML (ZoomDialogBox.xaml):
<StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center">
<Label>Factor:</Label>
<TextBox Name="FactorTextBox" Width="30" Margin="10,0,0,0">
<TextBox.Text>
<Binding Path="." UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validators:ZoomFactorValidationRule MinZoomFactor="1" MaxZoomFactor="20" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>This is the property I am binding to, and this binding works (from ZoomDialogBox.cs):
public int ZoomFactor
{
get
{
return (int) this.DataContext;
}set { this.DataContext = value; } }
As said, the binding works, but the ValidationRules is never applied. What am I doing wrong here?
"When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
-Atlas Shrugged, Ayn Rand -
I have a problem with ValidationRules. Basically the data binding works, but the Validate-method of the ValidationRules never fire. This is part of my XAML (ZoomDialogBox.xaml):
<StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center">
<Label>Factor:</Label>
<TextBox Name="FactorTextBox" Width="30" Margin="10,0,0,0">
<TextBox.Text>
<Binding Path="." UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validators:ZoomFactorValidationRule MinZoomFactor="1" MaxZoomFactor="20" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>This is the property I am binding to, and this binding works (from ZoomDialogBox.cs):
public int ZoomFactor
{
get
{
return (int) this.DataContext;
}set { this.DataContext = value; } }
As said, the binding works, but the ValidationRules is never applied. What am I doing wrong here?
"When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
-Atlas Shrugged, Ayn RandGive this short article a read. Your missing the NotifyOnValidationError property setting. http://blog.spencen.com/2007/10/04/wpf-validation-rules.aspx[^]
Cheers, Karl
» CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.
-
Give this short article a read. Your missing the NotifyOnValidationError property setting. http://blog.spencen.com/2007/10/04/wpf-validation-rules.aspx[^]
Cheers, Karl
» CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.
I did try that earlier (and now again), and the result is the same. Validate() never fires!
"When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
-Atlas Shrugged, Ayn Rand -
I have a problem with ValidationRules. Basically the data binding works, but the Validate-method of the ValidationRules never fire. This is part of my XAML (ZoomDialogBox.xaml):
<StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center">
<Label>Factor:</Label>
<TextBox Name="FactorTextBox" Width="30" Margin="10,0,0,0">
<TextBox.Text>
<Binding Path="." UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validators:ZoomFactorValidationRule MinZoomFactor="1" MaxZoomFactor="20" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>This is the property I am binding to, and this binding works (from ZoomDialogBox.cs):
public int ZoomFactor
{
get
{
return (int) this.DataContext;
}set { this.DataContext = value; } }
As said, the binding works, but the ValidationRules is never applied. What am I doing wrong here?
"When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
-Atlas Shrugged, Ayn RandYour ZoomFactor property should be turned into a property with a backing field. There are many different ways for the TextBox to bind to the ZoomFactor property. One way is in the constructor of the ZoomDialogBox.cs file, use this line of code
this.DataContext = this;
Then change the Path in the TextBox to ZoomFactor. You should also look at the VS Output window to see any data binding errors when your application is running.Cheers, Karl
» CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.
-
Your ZoomFactor property should be turned into a property with a backing field. There are many different ways for the TextBox to bind to the ZoomFactor property. One way is in the constructor of the ZoomDialogBox.cs file, use this line of code
this.DataContext = this;
Then change the Path in the TextBox to ZoomFactor. You should also look at the VS Output window to see any data binding errors when your application is running.Cheers, Karl
» CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.
Yes, yes yes! Thank you. Now it works. :):) And thanks for the tip on the Output window, I did not know that databinding errors was shown there. That can be a great time-saver. Once again, thanks for the help.
"When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
-Atlas Shrugged, Ayn Rand -
Yes, yes yes! Thank you. Now it works. :):) And thanks for the tip on the Output window, I did not know that databinding errors was shown there. That can be a great time-saver. Once again, thanks for the help.
"When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
-Atlas Shrugged, Ayn RandGlad you're up and running. Have a great day!
Cheers, Karl
» CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.