I'm working on a simple WPF application and I'm stucked on something I'm sure very simple but I cannot find the solution even after many searchs.
The question is about a validation rule on a TextBox Text property binding.
I'd like to simply generate a message when the text entered in the textbox isn't validated.
I followed those two pages on the subject:
http://msdn.microsoft.com/fr-fr/library/ms752347(v=vs.110).aspx
http://www.codeproject.com/Articles/15239/Validation-in-Windows-Presentation-Foundation
but I cannot find where I'm wrong.
Here's a sample of my code:
XAML part :
<TextBox x:Name="deviceIPAddressTextBox" HorizontalAlignment="Left" Height="23" Margin="109,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" FontStyle="Italic">
<TextBox.Text>
<Binding Path="Address" UpdateSourceTrigger="LostFocus" Mode="TwoWay">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Code part:
public partial class MainWindow : Window
{
public Device CurrentDevice;
public MainWindow()
{
CurrentDevice = new Device();
InitializeComponent();
deviceIPAddressTextBox.DataContext = CurrentDevice;
}
with Device class like that:
public class Device : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _Address;
public string Address
{
get { return _Address; }
set
{
if (string.IsNullOrEmpty(value)
{
_Address = "Enter IP Address";
OnPropertyChanged("Address");
return;
}
IPAddress ipAddress;
if (IPAddress.TryParse(value, out ipAddress))
{
_Address = value;
OnPropertyChanged("Address");
}
else
{
throw new ApplicationException("Not valid IP");
}
}
}
public Device()
{
Address = "Enter IP Address";
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Following the different tutos I read when firing the ApplicationException I should have something like the TextBox border red (WPF default) but instead I've got a classic "Not handled exception"
Could you please help me on that?
Many thanks.
Update 1 : Part of answer
Even if I have the Visual Studio "Unhandled exception" I actually have the excepted behavior on the UI... so the question is How to manage correctly the exception throw ?