0

I wonder how would it be possible to bind Window.Effect to ViewModel so that it could be the ViewModel controlling how the window looks like.

I'm aware that we can bind properties one-by-one such as following; however, this is not really binding Effect rather is binding settings to the Effect which is not of my interest.

<Window.Effect>
     <BlurEffect Radius="{Binding UIElementEffect.Radius}" />
</Window.Effect>
Dr. Strangelove
  • 2,725
  • 3
  • 34
  • 61
  • There's some really interesting chat about this kind of thing here: http://stackoverflow.com/questions/1896612/are-wpf-related-properties-inside-a-viewmodel-a-violation-of-mvvm-best-practices . The gist of it is that it's probably a bad idea to put an actual UI object in your ViewModel, but you could use an abstraction of one and run it through a converter to populate your Effect's properties. – goobering Apr 29 '15 at 21:52
  • You can do whatever you want, but if you're doing UI tasks in your VM that's not MVVM. –  Apr 30 '15 at 15:04
  • 1
    @Will Disagree. VM is a model of view (model of window or model of other UI element), it's supposed to "do UI tasks". – romanoza May 01 '15 at 08:03
  • @roman There's no need to disagree. The pattern is well defined and well documented. I'd suggest you go try finding support for your claim, as you can only benefit from such research. –  May 01 '15 at 13:29

1 Answers1

1

Why not just bind like this:

<Window
    ...
    Effect ="{Binding WindowEffect}">

?

As for "if it's a bad idea to put WPF/UI-specific object into ViewModel" and other "best-practices" and patterns I personally have a rule: I ask myself:
- What real problem will I get if I violate this best-practice (btw, are best-practices always best and best for everyone? ;) )? By real I mean real - not theoretical problems that someone has written in a book or some forum. Will these problems apply for my specific project? What is the chance this problem will really happen? Do disadvantages (problems that may actually never arise) outweigh the advantages of violating the "best-practice" (like simplicity, comfort of working with the code, etc.)?
I am not suggesting to violate best-practices, I am suggesting not to follow them blindly.

nightcoder
  • 13,149
  • 16
  • 64
  • 72