What is better to use? Instead of SelectionChanged use SelectedItem

private Game _SelectedItem; public Game SelectedItem { get { return _SelectedItem; } set { _SelectedItem = value; OnPropertyChanged(); System.Windows.MessageBox.Show(((Game)value).Creator); } } 

or so

 <ListBox ItemsSource="{Binding Games}" Background="#FF292D32" HorizontalContentAlignment="Stretch"> <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding ItemChanged}" CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}}" /> </i:EventTrigger> </i:Interaction.Triggers> 
  • Что лучше использовать? - what does it mean better? - tym32167
  • Well, probably what is more appropriate for the MVVM pattern to use. And is it generally correct to call a method in a property setter - Pavel Erikov
  • for the MVVM pattern, using bindings and commands are rules (another thing is that you could be attached much easier without a command). Calling OnPropertyChanged() in the setter is the norm. To call some UI things like a dialog box in the view model is not normal and is generally wrong. - tym32167 2:44
  • 1) And how easy it is to do (I just need so that if I change the Item, the method would be called where it would be passed, let's say the title of this item) 2) I’m aware that in the OnPropertyChanged () setter, I’ll call this on. And other methods? 3) And how to call the dialog box according to the rules mvvm? - Pavel Erikov
  • 1) Use binding to a property of something like <ListBox .... SelectedItem="{Binding SelectedItem}" .... 2) Setters and getters for all recommendations should be as simple as possible, the simpler the better. If you start writing logic noodles in setters, get unsupported mess in the code 3) Write a special class to display windows, call this class from the model - tym32167

0