WPF expert: Can you solve this problem?

  • Status: Closed
  • Prize: £25
  • Entries Received: 5
  • Winner: AlexDidenko

Contest Brief

Hello everyone,

Welcome to my first contest. I'm working for a while on a WPF project and I am currently facing a technical problem. I created a test application to be able to demonstrate what the problem is. See the attached file.

I'm facing a problem with WPF binding at the moment. The purpose of this project is to create a Validation that I can use through the whole application. The validation can be triggered by a Textbox or by a reusable UserControl (also to use for any model and any page in the application)

The models in the application (in the example we only have Patient) will inherit the validation from the base model. In the model the validation rules are defined with DataAnnotations. (this demo uses the INotifyDataErrorInfo interface)

You find a working example. If you fill in numbers that are out of range a validation Error will be displayed. This is exactly how I want it to work.

The problem I face: I created a work around to make it work. Have a look at the stylesheet BaseStyle xml:

<!-- 1. This binding should work for the UserControl and TextBox -->
<Trigger Property="Validation HasError" Value="True">
<Setter Property="BorderBrush" Value="Orange" TargetName="TextboxBorder" />
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource Self}, Path=(Validation Errors).CurrentItem ErrorContent}"/>
</Trigger>

<!-- 2. This binding works for the UserControl, but it is a bad workaround that I want to remove -->
<DataTrigger Binding="{Binding Path=(Validation HasError)}" Value="True">
<Setter Property="BorderBrush" Value="Orange" TargetName="TextboxBorder" />
<Setter Property="ToolTip" Value="{Binding (Validation Errors) CurrentItem ErrorContent}" />
</DataTrigger>

See the demo, the StartFormView xaml view has a TextBox and a UserControl.

When I use a TextBox the Validation error is being routed well and the Style of the first block above shows an error message.
When I use the UserControl the Validation error is being routed to the UserControl (that's what I assume). Because of this the first trigger of the code above will not be fired and no error message will be displayed.

As a workaround I created a DataTrigger (the second trigger above). This DataTrigger is in some kind of way able to access the Validation object. I have no logical explanation for that.

What I want is the UserControl to work without the DataTrigger. With the right binding the UserControl and the TextBox should work. I believe the solution is to be find be forwarding the Validation object from the UserControl in some kind of way to the TextBox within the UserControl.

Can you make this example work without using the DataTrigger? Since it's just a matter of binding I don't want any workarounds.

The winner will be the one with the best solution (clean code, no workaround), but also to the one that is able to describe the best what the actual problem is we face.


Extra task: When entering a non decimal value a convert error is generated. It would be nice if numbers or decimal values are automatically limited to number input only. To create this the ValueConverter NumbersOnlyConverter might be used, but feel free to use your own solution

Good luck! I'm very curious about your findings!

HOW TO SUBMIT:

The submission type of this contest was automatically set the JPG, PNG, GIF . I raised a ticket for this at freelancer customer support (I asked them to change the submission type to PDF).

The mean time workaround:
Just add a random image as submission. You can describe the project in the submission. After the submission we will be able to chat and share the project files.

Recommended Skills

Employer Feedback

“Alex is a skilled developer. Very motivated, thanks for the solution provided with the contest!”

Profile image LennartPoot, United Kingdom.

Top entries from this contest

View More Entries

Public Clarification Board

No messages yet.

How to get started with contests

  • Post your contest

    Post Your Contest Quick and easy

  • Get tons of entries

    Get Tons of Entries From around the world

  • Award the best entry

    Award the best entry Download the files - Easy!

Post a Contest Now or Join us Today!