I have a simple sample that my sample has 2 window :
1-ProductlistView 2-ProductEditView (1-ProductlistViewModel 2-ProductEditViewModel)
I want the user can select a product in my ProductlistView and edit selected product in ProductEditView ...i'm using from this code in my sample:
public Class ProductEditViewModel:ViewModelBase
{
private readonly ProductEditView View;
public ProductModel Model { get; set; }
public ProductEditViewModel(Product myproduct)
{
View = new ProductEditView { DataContext = this };
if(myproduct!= null) Model = myproduct;
}
private bool IsInDialogMode;
public bool? ShowDialog()
{
if (IsInDialogMode) return null;
IsInDialogMode = true;
return View.ShowDialog();
}
}
and write to my editCommant in ProductlistViewModel:
private RelayCommand UpdateProductmdInstance;
public RelayCommand UpdateProductCommand
{
get
{
if (UpdateProductmdInstance!= null) return UpdateProductmdInstance;
UpdateProductmdInstance= new RelayCommand(a => OpenProductDetail(SelectedProduct), p => SelectedProduct!= null);
return UpdateProductmdInstance;
}
}
private void OpenProductDetail(Product product)
{
var ProductEditViewModel= new ProductEditViewModel(product);
var result = personDetailViewModel.ShowDialog();
...
}
I was wondering my sample is wrong?
Can i have an instance from a view in its viewmodel?
If my Sample is wrong how can i do this solution(send an object to other window and after edit get it)?
It is normally recommended to NOT have a ViewModel referencing a View. See this question on how to show a dialog from ViewModel.
Related
Let's say I have a parameter in my ViewModel:
public string ChosenQualityParameter
{
get => DefectModel.SelectedQualDefectParameters?.Name ?? "Не выбран параметр";
}
and I have a class DefectModel with parameter SelectedQualDefectParameters.Name in it. I want to change the UI binded to ChosenQualityParameter, when theName parameter changes too.
But I don't know how to do this properly. Any suggestions? Thanks in advance.
You might define your ViewModel class like this:
public class ViewModel
{
private DefectModel _defectModel;
public ViewModel(DefectModel defectModel)
{
_defectModel = defectModel;
}
public string ChosenQualityParameter
{
get => _defectModel.SelectedQualDefectParameters?.Name ?? "Не выбран параметр";
}
}
I personally do not like such dependencies in viewmodels, but it might get the job done here. It seems to work in a console application anyway:
using System;
public class Parameters
{
public string Name { get; set; }
}
public class DefectModel
{
public Parameters SelectedQualDefectParameters { get; set; }
}
public class ViewModel
{
private DefectModel _defectModel;
public ViewModel(DefectModel defectModel)
{
_defectModel = defectModel;
}
public string ChosenQualityParameter
{
get => _defectModel.SelectedQualDefectParameters?.Name ?? "Не выбран параметр";
}
}
class Program
{
static void Main()
{
var defectModel = new DefectModel
{
SelectedQualDefectParameters = new Parameters
{
Name = "test"
}
};
var viewModel = new ViewModel(defectModel);
Console.WriteLine(viewModel.ChosenQualityParameter);
defectModel.SelectedQualDefectParameters.Name = "changed";
Console.WriteLine(viewModel.ChosenQualityParameter);
Console.ReadKey();
}
}
Thanks to #Knoop and #BartHofland, I've solved my issue by using INotifyPropertyChanged in my DefectModel and SelectedQualDefectParameters classes.
For setting ChosenQualityParameter I used MessagingCenter to send new value.
I'm currently working on a Xamarin.iOS project that uses a web-api to gather data. However, I'm running into some problems trying to pass the user input from a textfield to the Tableview that gets the result from the api.
To do this I've followed the example on the MvvmCross documentation.
The problem is that the input from the Textfield never reaches the 'Filter' property in my TableviewController's viewmodel. I think I'm not passing the string object correctly to my IMvxNavigationService when called.
To clarify, in my UserinputViewController I'm binding the textfield's text like so:
[MvxFromStoryboard(StoryboardName = "Main")]
public partial class SearchEventView : MvxViewController
{
public SearchEventView (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
MvxFluentBindingDescriptionSet<SearchEventView, SearchEventViewModel> set = new MvxFluentBindingDescriptionSet<SearchEventView, SearchEventViewModel>(this);
set.Bind(btnSearch).To(vm => vm.SearchEventCommand);
set.Bind(txtSearchFilter).For(s => s.Text).To(vm => vm.SearchFilter);
set.Apply();
}
}
The Viewmodel linked to this ViewController looks like this:
public class SearchEventViewModel : MvxViewModel
{
private readonly IMvxNavigationService _navigationService;
private string _searchFilter;
public string SearchFilter
{
get { return _searchFilter; }
set { _searchFilter = value; RaisePropertyChanged(() => SearchFilter); }
}
public SearchEventViewModel(IMvxNavigationService mvxNavigationService)
{
this._navigationService = mvxNavigationService;
}
public IMvxCommand SearchEventCommand {
get {
return new MvxCommand<string>(SearchEvent);
}
}
private async void SearchEvent(string filter)
{
await _navigationService.Navigate<EventListViewModel, string>(filter);
}
}
And finally, TableviewController's viewmodel looks like this:
public class EventListViewModel : MvxViewModel<string>
{
private readonly ITicketMasterService _ticketMasterService;
private readonly IMvxNavigationService _navigationService;
private List<Event> _events;
public List<Event> Events
{
get { return _events; }
set { _events = value; RaisePropertyChanged(() => Events); }
}
private string _filter;
public string Filter
{
get { return _filter; }
set { _filter = value; RaisePropertyChanged(() => Filter); }
}
public EventListViewModel(ITicketMasterService ticketMasterService, IMvxNavigationService mvxNavigationService)
{
this._ticketMasterService = ticketMasterService;
this._navigationService = mvxNavigationService;
}
public IMvxCommand EventDetailCommand {
get {
return new MvxCommand<Event>(EventDetail);
}
}
private void EventDetail(Event detailEvent)
{
_navigationService.Navigate<EventDetailViewModel, Event>(detailEvent);
}
public override void Prepare(string parameter)
{
this.Filter = parameter;
}
public override async Task Initialize()
{
await base.Initialize();
//Do heavy work and data loading here
this.Events = await _ticketMasterService.GetEvents(Filter);
}
}
Whenever trying to run, the string object 'parameter' in my TableviewController's Prepare function remains 'null' and I have no idea how to fix it. Any help is greatly appreciated!
I believe the issue is with your command setup
new MvxCommand<string>(SearchEvent);
As this command is being bound to a standard UIButton. It will not pass through a parameter value of your filter but null instead. So the string parameter generic can be removed. Additionally, as you want to execute an asynchronous method I would suggest rather using MvxAsyncCommand
new MvxAsyncCommand(SearchEvent);
Then in terms of SearchEvent method you can remove the parameter. The value of filter is bound to your SearchFilter property. It is this property's value that you want to send as the navigation parameter.
private async Task SearchEvent()
{
await _navigationService.Navigate<EventListViewModel, string>(SearchFilter);
}
I have got my MainWindow which loads new UserControls and there ViewModel's into it's ContentControl, so the Views are switched.
However, I need to access a property in my MainWindow ViewModel from a ViewModel within the ContentControl.
MainWindowViewModel
namespace PhotoManagement
{
public class MainWindowViewModel : NotifyUIBase
{
public ObservableCollection<ViewVM> Views { get; set; }
private ObservableCollection<Logged> loggedUsers;
public ObservableCollection<Logged> LoggedUsers
{
get
{
return loggedUsers;
}
set
{
loggedUsers.Add(value[0]);
//There is a user logged in, switch to home and display menu
if (loggedUsers.Count > 0)
{
//Display menu, switch Windows
MessageBox.Show("Someone is logged in!");
}
else
{
MessageBox.Show("No-one is logged in!");
}
}
}
Below you can see the LoginViewModel which is in the MainWindow ContentControl, I have added a comment where i'm trying to add this new user to the ObservableCollection.
#region Login Methods
private LoginVM loginVM;
public LoginVM LoginVM
{
get
{
return loginVM;
}
set
{
loginVM = value;
editEntity = editVM.TheEntity;
RaisePropertyChanged();
}
}
protected override void DoLogin()
{
//Check if email exists
var exist = db.Users.Count(a => a.Email == LoginVM.TheEntity.Email);
if (exist != 0)
{
//Fecth user details
var query = db.Users.First(a => a.Email == LoginVM.TheEntity.Email);
if (Common.Security.HashGenerator.CalculateHash(LoginVM.TheEntity.ClearPassword, query.Salt) == query.Hash)
{
//Password is correct
MessageBox.Show("Details correct!");
//Set properties
LoginVM.TheEntity.FirstName = query.FirstName;
LoginVM.TheEntity.LastName = query.LastName;
LoginVM.TheEntity.UID = query.UID;
//Add the LoginVM to LoggedUsers
Edit:
This is where I add the Views in MainWindowViewModel
namespace PhotoManagement
{
public class MainWindowViewModel : NotifyUIBase
{
public ObservableCollection<ViewVM> Views { get; set; }
private ObservableCollection<Logged> loggedUsers;
public ObservableCollection<Logged> LoggedUsers
{
get
{
return loggedUsers;
}
set
{
loggedUsers.Add(value[0]);
//There is a user logged in, switch to home and display menu
if (loggedUsers.Count > 0)
{
//Display menu, switch Windows
MessageBox.Show("Someone is logged in!");
}
else
{
MessageBox.Show("No-one is logged in!");
}
}
}
public string Version
{
get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); }
}
public MainWindowViewModel()
{
ObservableCollection<ViewVM> views = new ObservableCollection<ViewVM>
{
new ViewVM { IconGeometry=App.Current.Resources["home4"] as Geometry, ViewDisplay="Home", ViewType = typeof(LoginView), ViewModelType = typeof(LoginViewModel)},
new ViewVM { IconGeometry=App.Current.Resources["instagram3"] as Geometry, ViewDisplay="Images", ViewType = typeof(LoginView), ViewModelType = typeof(LoginView)},
new ViewVM { IconGeometry=App.Current.Resources["money674"] as Geometry, ViewDisplay="Sales", ViewType = typeof(LoginView), ViewModelType = typeof(LoginViewModel)},
new ViewVM { IconGeometry=App.Current.Resources["printing1"] as Geometry, ViewDisplay="Print Queue", ViewType = typeof(LoginView), ViewModelType = typeof(LoginViewModel)},
new ViewVM { IconGeometry=App.Current.Resources["cog2"] as Geometry, ViewDisplay="Settings", ViewType = typeof(IconLibaryView), ViewModelType = typeof(IconLibaryViewModel)},
new ViewVM { IconGeometry=App.Current.Resources["upload40"] as Geometry, ViewDisplay="Upload", ViewType = typeof(IconLibaryView), ViewModelType = typeof(IconLibaryViewModel)}
};
Views = views;
RaisePropertyChanged("Views");
views[0].NavigateExecute();
}
}
}
You simply need to use Ancestor binding from within ContentControl's any child element :
{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},Path=DataContext.AnyPropertyOfMainWindowViewModel}
If Window has MainWindowViewModel as DataContext.
I would go with events for ViewModel-to-ViewModel communication, and I prefer the IEventAggregator, available as a PubSub nuget package from Microsoft, but there are plenty to choose from (or roll your own if you prefer).
public MainViewModel() {
Aggregator.GetEvent<UserLoggedInEvent>().Subscribe(user => ...do your magic);
}
And in your LoginViewModel, publish it after the user has logged in:
public DoLogin() {
... do other stuff here...
Aggregator.GetEvent<UserLoggedInEvent>().Publish(userDetails);
}
Using the IEventAggregator from Prism, the event class is simple:
public class UserLoggedInEvent : PubSubEvent<User> {}
Btw - One of the main purposes for MVVM or any design pattern is to abstract UI from business code, so if you can remove all your App.Current.Resources stuff from your VM using a converter or something else then you've abstracted it from WPF (much more easily ported to other platforms like UWP).
I have a problem using the IDataErrorInfo in combination with IReactiveBinding.Bind(). I hope someone here can help me.
I have a ViewModel that is inherited from ReactiveObject and implements the IDataErrorInfo interface.
public class MainWindowViewModel : ReactiveUI.ReactiveObject, IDataErrorInfo
{
private string username = string.Empty;
public string Username
{
get { return this.username; }
set { this.RaiseAndSetIfChanged(ref this.username, value); }
}
public MainWindowViewModel()
{
this.Validator = new MainWindowViewModelValidator();
}
public AbstractValidator<MainWindowViewModel> Validator { get; set; }
#region IDataErrorInfo Members
string IDataErrorInfo.Error
{
get
{
return Validator != null ? string.Join(Environment.NewLine, Validator.Validate(this).Errors.Select(x => x.ErrorMessage).ToArray())
: string.Empty;
}
}
string IDataErrorInfo.this[string propertyName]
{
get
{
if (Validator != null)
{
var results = Validator.Validate(this, propertyName);
if (results != null
&& results.Errors.Count() > 0)
{
var errors = string.Join(Environment.NewLine, results.Errors.Select(x => x.ErrorMessage).ToArray());
return errors;
}
}
return string.Empty;
}
}
#endregion
}
The MainWindowViewValidator ensures that the Usernameproperty is not empty.
The ViewModel is connected to the View in the code behind of the XAML-File:
public partial class MainWindow : IViewFor<MainWindowViewModel>
{
public MainWindow()
{
InitializeComponent();
this.ViewModel = new MainWindowViewModel();
this.Bind(this.ViewModel, viewmodel => viewmodel.Username, view => view.Username.Text);
}
public MainWindowViewModel ViewModel
{
get { return (MainWindowViewModel)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register("ViewModel", typeof(MainWindowViewModel), typeof(MainWindow), new PropertyMetadata(null));
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (MainWindowViewModel)value; }
}
}
The problem is now that the model validation is not called, as I don't specify the databinding in the XAML file directly.
Does anybody has a neat solution for this problem?
The problem is now that the model validation is not called, as I don't specify the databinding in the XAML file directly.
ReactiveUI doesn't participate in IDataErrorInfo and friends for binding. It used to, but really if you think about it, Validation itself is a derived property of the form.
ReactiveUI is already really good at describing how properties are related to each other (via WhenAny/ToProperty), so you should just construct an ValidationError property that displays the error message.
I'm developing simple application in WPF with MVVM Light Toolkit. I have two views:
HomeView (default)
CustomersView
This is part of the MainViewModel class:
public MainViewModel()
{
CurrentViewModel = Bootstrapper.Instance.Container.Resolve<HomeViewModel>();
}
private void ExecuteShowCustomersCommand()
{
CurrentViewModel = Bootstrapper.Instance.Container.Resolve<CustomersViewModel>();
}
In CustomerViewModel I have property:
public ObservableCollection<Customers> Customers
{
get { return _customers; }
set
{
if (_customers == value) return;
_customers = value;
RaisePropertyChanged(CustomersPropertyName);
}
}
And my question is, when I should call the web service to get customers data? In CustomerViewModel constructor?
I would do it in the constructor of the viewmodel and use a IoC Container to get the instance.
Application start
SimpleIoc.Default.Register<IDataService, DataService>();
SimpleIoc.Default.Register<MyViewModel>();
ViewModel
public MyViewModel(IDataService DataService)
{
Mydata = DataService.GetData(); // Edit: Could also be done in a property with lazy load
}
Locator
public MyViewModel MyVM
{
get
{
return SimpleIoc.Default.GetInstance<MyViewModel>();
}
}