Wednesday, 14 August 2013

Validation of a bound C# format string in WPF

Validation of a bound C# format string in WPF

Please consider the following fragment...
string RateDisplayFormat = "###,###,##0.00000";
// lots of code goes here...
double rate = 123.4567;
string rateDisplay = rate.ToString(RateDisplayFormat);
One of the requirements is that the format string be end-user
configurable. So it is presented in a user settings control like this...
<StackPanel Orientation="Horizontal">
<TextBlock Text="Rate Display"/>
<TextBox Text="{Binding RateDisplayFormat}"/>
</StackPanel>
I would like to apply a preflight automated validation to their entry; and
if it is wildly inappropriate, to refuse an update. I could show a number
formated on their entry, but it's valuable real estate and it makes the
validation passive rather than assertive.
I have considered a drop down which contains all reasonable values, but
rejected the idea because users will invariably want to do something not
in the list.
So I am now exploring the possibility of a ValidationRule. And the
question is: what do you put in the validation logic to provide assurance
that the user's input (e.g., ###,00) can be consumed by C#'s ToString
method? I am not a RegEx guy, but happy to accept a well formed
expression. Secondarily, if WPF's ValidationRule is not the way to wrap
the logic, I would be interested in that also.
For those needing deliberation, I promise not to accept until cob Friday.

No comments:

Post a Comment