Decimal received in Int Property - breaks rules?

Decimal received in Int Property - breaks rules?

Old forum URL: forums.lhotka.net/forums/t/6328.aspx


davido posted on Wednesday, February 04, 2009

Hi,

I am using CSLA light and have the following scenario. If i enter -9 on the screen and tab away I see the red exclamation and the correct message. If I now overwrite -9 with 12 and tab away the red exclamation goes away. However if I overwrite the -9 with 12.5 and tab away the red exclamation stays. The property is Int so does entering a decimal give it a problem?

I put a breakpoint on the setter but it doesn't get there when I enter 12.5, it gets there when I enter an integer.

private static PropertyInfo<int> MileageProperty = RegisterProperty<int>(new PropertyInfo<int>("Mileage", "Mileage", 0));

public int Mileage

{

get { return GetProperty(MileageProperty); }

set { SetProperty(MileageProperty, value); }

}

ValidationRules.AddRule(Csla.Validation.CommonRules.IntegerMinValue,

new Csla.Validation.CommonRules.IntegerMinValueRuleArgs(MileageProperty, 0));

davido replied on Wednesday, February 04, 2009

I should have mentioned I am using the CslaDataProvider

davido replied on Wednesday, February 04, 2009

I have noticed the following in the output window:

System.Windows.Data Error: ConvertBack cannot convert value 'sadasd' (type 'System.String'). BindingExpression: Path='Data.Mileage' DataItem='Csla.Silverlight.CslaDataProvider' (HashCode=27451669); target element is 'System.Windows.Controls.TextBox' (Name='MileageTextBox'); target property is 'Text' (type 'System.String').. System.FormatException: Input string was not in a correct format.

I'm off to investiagte how to capture a ConvertBack exception from a default ValueConverter

ajj3085 replied on Wednesday, February 04, 2009

You might be better off trying to limit the input to only digit characters.

davido replied on Wednesday, February 04, 2009

By adding the following event handler and settings in the binding I am getting an error raised, which is good, one step at a time. I'll see how I can work with this

public ExpenseUpdatePage()

{

InitializeComponent();

BindingValidationError += new EventHandler<ValidationErrorEventArgs>(ExpenseUpdatePage_BindingValidationError);

}

void ExpenseUpdatePage_BindingValidationError(object sender, ValidationErrorEventArgs e)

{

throw new NotImplementedException();

}

 

<TextBox

x:Name="MileageTextBox"

Style="{StaticResource TextBoxStyle}"

Grid.Row="9"

Grid.Column="1">

<TextBox.Text>

<Binding

Path="Data.Mileage"

Mode="TwoWay"

NotifyOnValidationError="True"

ValidatesOnExceptions="True" />

</TextBox.Text>

davido replied on Wednesday, February 04, 2009

This seems to work but I'd appreciate any comments:

void ExpenseUpdatePage_BindingValidationError(object sender, ValidationErrorEventArgs e)

{

CslaDataProvider provider = ((CslaDataProvider)this.Resources["ExpenseData"]);

TextBox textBox = e.OriginalSource as TextBox;

if (textBox != null)

{

switch (e.Action)

{

case ValidationErrorEventAction.Added:

textBox.SelectionStart = 0;

textBox.SelectionLength = textBox.Text.Length;

textBox.FontStyle = FontStyles.Italic;

//this effectivly removes the binding

SaveButton.IsEnabled = false;

textBox.Focus();

break;

case ValidationErrorEventAction.Removed:

//todo reset style

System.Windows.Data.Binding binding = new System.Windows.Data.Binding();

binding.Source = provider.CanSave;

binding.Mode = System.Windows.Data.BindingMode.OneWay;

SaveButton.SetBinding(Button.IsEnabledProperty, binding);

break;

default:

break;

}

}

}

Copyright (c) Marimer LLC