开发者

MVVM and Custom Controls?

开发者 https://www.devze.com 2023-04-11 01:18 出处:网络
I\'m working on PRISM application with modules, MVVM and so on. I understand PRISM pretty good now and I understand value of MVVM. All those things good to deliver business value which comes from test

I'm working on PRISM application with modules, MVVM and so on. I understand PRISM pretty good now and I understand value of MVVM. All those things good to deliver business value which comes from testability, "uniformity" and so on.

But now I'm stuck with certain interaction issues. I already spent hours and hours trying to see how I set focus in Silverlight via MVVM. All this additional behaviors, attached properties, triggers. It just seems like bunch of junk code with MVVM 开发者_Python百科being root cause.

For example, I need to create Lookup control which is basically textbox with button and popup window. This control itself needs lot of focus control, it needs to overlay view over parent (popups) and so on. It seems to be pretty easy to create it with code-behind, stick it into separate library and move on. My business forms will use this control inside my nice MVVM PRISM.

So, question is.. Is it justified to use code-behind in isolated islands like controls and keep MVVM and TDD for actual code that brings business value?

Is there line where you say "MVVM is not going to be used here" ?


I see absolutely nothing wrong with using Code Behind providing that the code is related to view-specific properties, such as setting Focus. Your ViewModel should never need to know about or care who or what has focus, since that is a View-Specific concept.

Usually I build UserControls in two ways: they are either built for a specific Model or ViewModel, or they are meant to be generic and have their values provided by whoever calls them.

In the case of the former, such as if I wanted a SearchResultsPopup, I would build the UserControl expecting to have something like a SearchResultsViewModel as the DataContext.

For example, my UserControl would expect to find the following properties on it's DataContext, and would use them in bindings to build the View.

  • ObservableCollection<SearchResult> Results
  • SearchResult SelectedResult
  • bool IsOpen
  • ICommand OkCommand
  • ICommand CancelCommand

I could then use the UserControl like this:

<local:SearchResultsPopup DataContext="{Binding MySearchResultsVM}" />

In the later situation, where I am creating something generic which can be used by any Model or ViewModel, I would use custom Dependency Properties to provide my UserControl with the values it needs to bind to.

So in this example, I would have DependencyProperties for

  • bool IsOpen
  • ICommand OkCommand
  • ICommand CancelCommand

And my XAML would look something like this:

<local:GenericPopup local:GenericPopup.IsOpen="{Binding IsPopupOpen}"
                    local:GenericPopup.SaveCommand="{Binding SavePopupCommand}"
                    local:GenericPopup.CancelCommand="{Binding HidePopupCommand}">

    <local:MySearchResultsView ... />
</local:GenericPopup>

In summary, your UserControl is either a reflection of your ViewModel (meaning it becomes a View), or it is provided values by the View. The ViewModel doesn't care either way.

0

精彩评论

暂无评论...
验证码 换一张
取 消