4

I am creating User Control. I created three dependency properties which the user should bind to a viewmodel in XAML.

Is there something I can do so if developer does not bind the property? Ideally, the project would not compile successfully.

3
  • Throw exception, for example, why not? Commented Dec 9, 2015 at 8:23
  • Set a default values for your dependency properties. There nothing you can do to fail the build, if there is no errors. Commented Dec 9, 2015 at 9:14
  • 5
    I absolutely agree with this question. It would be nice to get the same sort of compile-time behavior in xaml as you can get in code. Commented Jun 8, 2016 at 20:02

1 Answer 1

1

This is impossible. Bindings are only checked at run-time, not at compile-time. The reason for this is that the compiler cannot determine what datacontext you wish to set for a view while the application is running since you can always change the datacontext.

If you're interested you can look up the MVVM design pattern to learn more about the reason why the model and view are seperated.

Sign up to request clarification or add additional context in comments.

2 Comments

Bindings are only checked at run-time but that's not the question. The question was about not having a binding at all, not whether it doesn't evaluate to something.
@GeorgeMauer The question explicitly ask to check at compile time if a binding is used. Since a binding is only checked at run-time this is imposible. At least I cannot find any other interpretation of "Is there something I can do so if developer does not bind the property? Ideally, the project would not compile successfully."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.