开发者

ViewModel responsibility and poping up a child window in Silverlight

开发者 https://www.devze.com 2023-02-01 03:46 出处:网络
Where to start? Question is about two different ways to achieve the same thing, and both smell to me, so I need somebody to tell me whether there is a better way. So, we have a screen that displays da

Where to start? Question is about two different ways to achieve the same thing, and both smell to me, so I need somebody to tell me whether there is a better way. So, we have a screen that displays data in data grid. When user clicks an icon in data grid, a modal pop up window (from Prism) is displayed. This can be done two ways, that I know of, working with SL for 2 weeks. To give some more background, we use MVVM pattern, and we have a ViewModelLocator.

Button in datagrid per each row

<Button Width="16" Name="cmdEdit" Height="16" Margin="10,0,0,0" Style="{StaticResource ImageButtonStyle}" Click="cmdEdit_Click" CommandParameter="{Binding}">
         <Button.Content>
             <Image  Source="/Test.Application.Bid;component/Images/edit.png"/>
          </Button.Content>
</Button>

First way, handle click from code behind:

var p = new PopupChildWindowAction();
 var vml = new ViewModelLocator();
 var viewModel = vml["BidAgentEditView"] as BidAgentEditViewModel;
 var view = new BidAgentEditView();
 view.DataContext = viewModel;
 viewModel.BidAgent = ((Button) e.OriginalSource).CommandParameter as BidAgentD开发者_JAVA百科to;
 p.ChildWindow = view;
 p.ChildWindow.Show();

Second way is to use InteractionRequestTriggers in xaml, and commands on view model like this

button in data grid

 <Button Width="16" Height="16" Margin="10,0,0,0" Style="{StaticResource ImageButtonStyle}" 
         prism:Click.Command="{Binding Source={StaticResource cc}, Path=DataContext.EditBidAgentCommand}" 
         prism:Click.CommandParameter="{Binding}">

in viewmodel ctor

this._editBidAgentRequest = new InteractionRequest<BidAgentEditViewModel>();

EditBidAgentCommand = new DelegateCommand<BidAgentDto>(editBidAgent, canEditBidAgent);

and handler for command

 private void editBidAgent(BidAgentDto bidAgent)
 {
     _editBidAgentRequest.Raise(newBidAgentEditViewModel(bidAgent,_bidAgentDataService));
  }

So view invokes command on the view model that turns around and raises interaction request that is than handled by the view and it pops up the child window. Maybe I don't understand MVVM, but this seems to be overly complicated way to handle user interaction. Plus that fact that I have something calles "Interaction" on view model makes me feel dirty. What do you think, is there a better way to do this without having to declare these interaction requests on the view model, something that is done completely in xaml, without having to handle this in view model?

From code behind first to grasp what is going on:


As you probably know, Prism proposes interaction requests for this kind of scenario. Remember, that the VM must handle the business logic part of the view. Ordering to show a popup when an action occurs is part of the VMs responsibilities.

Additionally, you keep your View clean and without any codebehind (which is otherwise almost untestable), plus you are using the benefits of Silverlight to your advantage.

Summing up, interaction requests seem pretty neat to me.

0

精彩评论

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