Thanks to some of the advice I have got previously on Stack Overflow I have been making good progress in my understanding of MVVM. However, it is when things start to get more complicated that I am still struggling.
I have the view below which is for the purpose of entering orders. It is bound to a DataContext of OrderScreenViewModel.
<StackPanel>
<ComboBox Height="25" Width="100" DisplayMemberPath="CustomerCode" SelectedItem="{Binding Path=Order.Customer}" ItemsSource="{Binding Path=Customers}"></ComboBox>
<ComboBox Height="25" Width="100" DisplayMemberPath="ProductCode" SelectedItem="{Binding Path=CurrentLine.Product}" ItemsSource="{Binding Path=Products}"></ComboBox>
</StackPanel>
The first combobox is used to select a Customer. The second combobox is used to select a ProductCode for a new OrderLine.
There are the items that I cannot work out how to achieve in MVVM:
1) When a Customer is selected update the Products combobox so that its item source only shows Products that have the same CustomerId as the CustomerDto record selected in the combobox
2) When Load is called set the SelectedItem in the Customers combobox so that it displays the Customer with the CustomerId equal to the one on the OrderDto.
3) Apply, the same process as 1) so that only Products belonging to that Customer are shown / loaded and set the SelectedItem on the Products combobox so that it is pointing to the entry with the same ProductId as is contained on the OrderLineDto
I am not sure how to proceed or even if I have got the responsibilities of my viewmodels correct. Maybe it has something to do with NotifyPropertyChanged? Any pointers on how I can achieve the above will be greatly appreciated. I am sure if I get this right it will help me greatly in my app. Many thanks Alex.
public class OrderScreenViewModel
{
public WMSOrderViewModel Order { get; private set; }
public WMSOrderLineViewModel CurrentLine { get; private set; }
public OrderScreenViewModel()
{
Order = new WMSOrderViewModel();
CurrentLine = new WMSOrderLineViewModel(new OrderLineDto());
}
public void Load(int orderId)
{
var orderDto = new OrderDto { CustomerId = 1, Lines = new List<OrderLineDto> { new OrderLineDto{ProductId = 1 }} };
Order = new WMSOrderViewModel(orderDto);
}
public List<CustomerDto> Customers
{
get{
return new List<CustomerDto> {
new CustomerDto{CustomerId=1,CustomerCode="Apple"},
new CustomerDto{CustomerId=1,CustomerCode="Microsoft"},
};
}
}
public List<ProductDto> Products
{
get
{
return new List<ProductDto> {
new ProductDto{CustomerId=1,ProductId=1,ProductCode="P100",Description="Pepsi"},
new ProductDto{CustomerId=1,ProductId=2,ProductCode="P110",Description="Coke"},
new ProductDto{CustomerId=2,ProductId=3,ProductCode="P120",Description="Fanta"},
new ProductDto{CustomerId=2,ProductId=4,ProductCode="P130",Description="Sprite"}
};
}
}
public class WMSOrderLineViewModel
{
private ProductDto _product;
private OrderLineDto _orderLineDto;
public WMSOrderLineViewModel(OrderLineDto orderLineDto)
{
_orderLineDto = orderLineDto;
}
public ProductDto Product { get { return _product; }
set{_product = value; RaisePropertyChanged("Product"); }
}
public class WMSOrderViewModel
{
private ObservableCollection<WMSOrderLineViewModel> _lines;
private OrderDto _orderDto;
public ObservableCollection<WMSOrderLineViewModel> Lines { get { return _lines; } }
private CustomerDto _customer;
public CustomerDto Customer { get{return _customer;} set{_customer =value; RaisePropertyChanged("Customer") }
public WMSOrderViewModel(OrderDto orderDto)
{
_orderDto = orderDto;
_lines = new ObservableCollection<WMSOrderLineViewModel>();
foreach(var lineDto in orderDto.Lines)
{
_lines.Add(new WMSOrderLineViewModel(lineDto));
}
}
public WMSOrderViewModel()
{
_lines = new ObservableCollection<WMSOrderLineViewModel>();
}
}
You need to make Products and Customers type ObservableCollection.
When you change these observablecollections in your viewmodel they will update the view, because OC's already implement INotifyPropertyChanged.
Order and CurrentLine should just be a type and not really be called a ViewModel.
1) You're going to have to do this when the setter is called on the SelectedItem of the Customer combobox is selected.
2) You'll need to do this probably in the ctr of the OrderScreenViewModel by using your logic to determine what Customer to change the CurrentLine.Customer too. If you do this in the ctr, this will set the value before the binding takes place.
3) Again, as long as you make changes to the ObservableCollection the combobox is bound to, it will update the UI. If you make a change to what the SelectedItem is bound to just make sure you call RaisedPropertyChanged event.
ETA: Change the xaml to this, bind to SelectedProduct and SelectedCustomer for the SelectedItem properties
<StackPanel>
<ComboBox Height="25" Width="100" DisplayMemberPath="CustomerCode" SelectedItem="{Binding Path=SelectedCustomer}" ItemsSource="{Binding Path=Customers}"></ComboBox>
<ComboBox Height="25" Width="100" DisplayMemberPath="ProductCode" SelectedItem="{Binding Path=SelectedProduct}" ItemsSource="{Binding Path=Products}"></ComboBox>
</StackPanel>
this should get you started in the right direction, everything, all logic for building customers and products by the customer id needs to happen in your repositories.
public class OrderScreenViewModel : INotifyPropertyChanged
{
private readonly IProductRepository _productRepository;
private readonly ICustomerRepository _customerRepository;
public OrderScreenViewModel(IProductRepository productRepository,
ICustomerRepository customerRepository)
{
_productRepository = productRepository;
_customerRepository = customerRepository;
BuildCustomersCollection();
}
private void BuildCustomersCollection()
{
var customers = _customerRepository.GetAll();
foreach (var customer in customers)
_customers.Add(customer);
}
private ObservableCollection<Customer> _customers = new ObservableCollection<Customer>();
public ObservableCollection<Customer> Customers
{
get { return _customers; }
private set { _customers = value; }
}
private ObservableCollection<Product> _products = new ObservableCollection<Product>();
public ObservableCollection<Product> Products
{
get { return _products; }
private set { _products = value; }
}
private Customer _selectedCustomer;
public Customer SelectedCustomer
{
get { return _selectedCustomer; }
set
{
_selectedCustomer = value;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedCustomer"));
BuildProductsCollectionByCustomer();
}
}
private Product _selectedProduct;
public Product SelectedProduct
{
get { return _selectedProduct; }
set
{
_selectedProduct = value;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedProduct"));
DoSomethingWhenSelectedPropertyIsSet();
}
}
private void DoSomethingWhenSelectedPropertyIsSet()
{
// elided
}
private void BuildProductsCollectionByCustomer()
{
var productsForCustomer = _productRepository.GetById(_selectedCustomer.Id);
foreach (var product in Products)
{
_products.Add(product);
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
public interface ICustomerRepository : IRepository<Customer>
{
}
public class Customer
{
public int Id { get; set; }
}
public interface IProductRepository : IRepository<Product>
{
}
public class Product
{
}
Here's what the standard IRepository looks like, this is called the Repository Pattern:
public interface IRepository<T>
{
IEnumerable<T> GetAll();
T GetById(int id);
void Save(T saveThis);
void Delete(T deleteThis);
}
Related
I use Prism. Data provider works correctly (SQLite here).
<ListBox ItemsSource="{Binding Path=CategoryList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public class ListsViewModel : BindableBase
{
private readonly IDataProvider _dataProvider;
private readonly IRegionManager _regionManager;
public DelegateCommand ClickAddCategory { get; set; }
private string categoryName;
public string CategoryName
{
get { return categoryName; }
set { SetProperty(ref categoryName, value); }
}
private ObservableCollection<ExtraCategory> categoryList;
public ObservableCollection<ExtraCategory> CategoryList
{
get
{
if (categoryList == null) return _dataProvider.GetCategoryList();
else return categoryList;
}
set { SetProperty(ref categoryList, value); }
}
public ListsViewModel(IRegionManager regionManager, IDataProvider dataProvider)
{
_dataProvider = dataProvider;
_regionManager = regionManager;
ClickAddCategory = new DelegateCommand(ClickedAddCategory);
//MessageBox.Show("Hello from " + this.ToString());
}
private void ClickedAddCategory()
{
ExtraCategory newCategoryFromForm = new ExtraCategory(CategoryName);
CategoryList.Add(newCategoryFromForm);
_dataProvider.AddCategory(newCategoryFromForm);
}
}
If I change the line:
CategoryList.Add(newCategoryFromForm);
to
CategoryList = _dataProvider.GetCategoryList();
everything would work fine because code inside set {} will run but that's not a solution. I would really appreciate some help. Also I really don't want to break MVVM pattern.
You want to put an ObservableCollection in a property without setter. And you want the value to stay the same.
private ObservableCollection<ExtraCategory> categoryList;
public ObservableCollection<ExtraCategory> CategoryList
{
get
{
if (categoryList == null) return _dataProvider.GetCategoryList();
else return categoryList;
}
set { SetProperty(ref categoryList, value); }
}
This gives you a new ObservableCollection everytime you call CategoryList.get, i.e. CategoryList.Add(newCategoryFromForm); fetches a new list, adds the new item, then discards the list (returning a value from a property's getter does not magically set a backing field).
I'd do it this way:
public ObservableCollection<ExtraCategory> CategoryList { get; }
and in constructor:
CategoryList = _dataProvider.GetCategoryList();
use and install nuget package PropertyChange.Fody.
Your class would look like this:
public class ListsViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private readonly IDataProvider _dataProvider;
private readonly IRegionManager _regionManager;
public DelegateCommand ClickAddCategory { get; set; }
public string CategoryName { get; set; }
public ObservableCollection<ExtraCategory> CategoryList { get; set; }
public ListsViewModel(IRegionManager regionManager, IDataProvider dataProvider)
{
_dataProvider = dataProvider;
_regionManager = regionManager;
ClickAddCategory = new DelegateCommand(ClickedAddCategory);
CategoryList = _dataProvider.GetCategoryList();
//MessageBox.Show("Hello from " + this.ToString());
}
private void ClickedAddCategory()
{
ExtraCategory newCategoryFromForm = new ExtraCategory(CategoryName);
CategoryList.Add(newCategoryFromForm);
_dataProvider.AddCategory(newCategoryFromForm);
}
}
I'm sure it will work perfectly
If what you want is to update the list use a method and bind a command to execute when you want to refresh
I have the following view model:
public sealed class FileViewModel : AbstractPropNotifier
{
private string _path;
private CategoryViewModel _category;
public string Path
{
get
{
return _path;
}
set
{
_path = value;
OnPropertyChanged(nameof(Path));
OnPropertyChanged(nameof(Title));
}
}
public string Title => System.IO.Path.GetFileNameWithoutExtension(Path);
public CategoryViewModel Category
{
get
{
return _category;
}
set
{
_category = value;
OnPropertyChanged(nameof(Category));
}
}
}
and Category view model:
public sealed class CategoryViewModel : IEquatable<CategoryViewModel>
{
public string Title { get; set; }
public EMyEnum Value { get; set; }
public bool Equals(CategoryViewModel other)
{
return Title.Equals(other.Title) && Value == other.Value;
}
public static CategoryViewModel From(EMyEnum eCat)
{
return new CategoryViewModel
{
Title = eCat.DescriptionAttr(),
Value = eCat
};
}
}
I set data context to my view model like:
public sealed class MainViewModel
{
public MainViewModel()
{
Files = new ObservableCollection<FileViewModel>();
Categories = GetCategories();
}
public ObservableCollection<FileViewModel> Files { get; set; }
public CategoryViewModel[] Categories { get; set; }
private CategoryViewModel[] GetCategories()
{
var enums = Enum.GetValues(typeof(EMyEnum));
var list = new List<CategoryViewModel>();
foreach (var en in enums)
{
EMyEnum cat = (EMyEnum)en;
list.Add(CategoryViewModel.From(cat));
}
return list.ToArray();
}
}
and
_model = new MainViewModel();
DataContext = _model;
and XAML:
<Window.Resources>
<CollectionViewSource x:Key="Categories" Source="{Binding Categories}"/>
</Window.Resources>
and in DataGrid element
<DataGridComboBoxColumn SelectedItemBinding="{Binding Category}" ItemsSource="{Binding Source={StaticResource Categories}}" Header="Category" Width="2*" DisplayMemberPath="Title"/>
The dropdown is populated correctly but cannot select automatically from dropdown a specific Category, means the Category column from Datagrid is empty.
I expected to select automatically from dropdown with correspondent Category...
Where is my mistake ? I tried with SelectedItemBinding and SelectedValueBinding but same issue. Nothing selected from dropdown.
To be clear:
For a file, I set a category but nothing is selected:
But dropdown has items:
There are probably different instances of CategoryViewModels in your MainViewModel compared to the ones in the FileViewModels.
You should either override Equals and GetHashCode in your CategoryViewModel class or make sure that you set the Category property of each FileViewModel to a CategoryViewModel that's actually present in the CategoryViewModel[] array of the MainViewModel.
I have a View that contains a ListView in which I load an ObservableCollection.
After that I am going to load some external data from our MySQL Database foreach item in my collection.
I would like to perform these in an async Task to prevent ui freezing.
The updating works fine for all items that are not visible in the ListView. Because they are virtualized?
The items that are currently on the ListView visible are not beeing updated. How can I notify the View to Update the Properties of the items?
Note: the Viewmodel implements InotifyPropertiChanged via a BaseViewModel
public class ProductViewModel : BaseTypes.BaseViewModel
{
public ProductViewModel()
{
}
public void GetProducts(string filter)
{
if (filter.Equals(""))
{
return;
}
Products.Clear();
Product product = new Product();
product.GetProductsByHersteller(filter);
if (product.productsList != null)
{
product.productsList.ToList().ForEach(Products.Add);
}
GetRanking();
}
private ObservableCollection<Product> _Products = new ObservableCollection<Product>();
public ObservableCollection<Product> Products { get { return _Products; } private set { _Products = value; } }
async Task GetRanking()
{
await Task.Run(() => GetRank());
}
private void GetRank()
{
foreach (Product item in Products)
{
item.Rank.Get(item.Asin);
NotifyPropertyChanged(nameof(Products));
}
}
}
Property in the Product class
private Produkt.Ranking rank = new Produkt.Ranking();
public Produkt.Ranking Rank { get => rank; set => rank = value; }
Solution:
Added INotifyPropertyChanged to the Ranking Class.
Code in the ViewModel now is this:
public void GetProducts(string filter)
{
if (filter.Equals(""))
{
return;
}
Products.Clear();
Product product = new Product();
product.GetProductsByHersteller(filter);
if (product.productsList != null)
{
product.productsList.ToList().ForEach(Products.Add);
}
GetRanking();
}
private ObservableCollection<Product> _Products = new ObservableCollection<Product>();
public ObservableCollection<Product> Products { get { return _Products; } private set { _Products = value; } }
async void GetRanking()
{
await Task.Run(() => GetRank());
}
private void GetRank()
{
foreach (Product item in Products)
{
item.Rank.Get(item.Asin);
}
}
Your Product class should implement INotifyPropertyChanged.
Then no need to call NotifyPropertyChanged in your GetRank() method.
I am trying to bind my ViewModel to my ComboBox. I have ViewModel class defined like this:
class ViewModel
{
public ViewModel()
{
this.Car= "VW";
}
public string Car{ get; set; }
}
I set this ViewModel as DataContext in Window_Load like:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = new CarModel();
}
Then in my xaml, I do this to bind my ComboBox to this ViewModel. I want to show the "VW" as selected by default in my ComboBox:
<ComboBox Name="cbCar" SelectedItem="{Binding Car, UpdateSourceTrigger=PropertyChanged}">
<ComboBoxItem Tag="Mazda">Mazda</ComboBoxItem>
<ComboBoxItem Tag="VW">VW</ComboBoxItem>
<ComboBoxItem Tag="Audi">Audi</ComboBoxItem>
</ComboBox>
I have 2 questions:
How do I set default value selected in Combo Box to "VW" (once form loads, it should show "VW" in combo box).
Instead of setting ComboBoxItems like above in xaml, how to I set it in my ViewModel and then load these in ComboBox?
Thanks,
UPDATE:
So far, I manage to implement this but I get error as below in the ViewModel c-tor:
namespace MyData
{
class ViewModel
{
public ViewModel()
{
this.Make = "";
this.Year = 1;
this.DefaultCar = "VW"; //this is where I get error 'No string allowed'
}
public IEnumerable<Car> Cars
{
get
{
var cars = new Car[] { new Car{Model="Mazda"}, new Car{Model="VW"}, new Car{Model="Audi"} };
DefaultCar = cars.FirstOrDefault(car => car.Model == "VW");
}
}
public string Make { get; set; }
public int Year { get; set; }
public Car DefaultCar { get; set; }
}
class Car
{
public string Model { get; set; }
}
}
As you are going to implement MVVM it will be a lot better if you start to think in objects to represent Cars in your application:
public class ViewModel
{
public Car SelectedCar{ get; set; }
public ObservableCollection<Car> Cars{
get {
var cars = new ObservableCollection(YOUR_DATA_STORE.Cars.ToList());
SelectedCar = cars.FirstOrDefault(car=>car.Model == "VW");
return cars;
}
}
}
public class Car
{
public string Model {get;set;}
public string Make { get; set; }
public int Year { get; set; }
}
Your Xaml:
<ComboBox SelectedItem="{Binding SelectedCar}", ItemsSource="{Binding Cars}"
UpdateSourceTrigger=PropertyChanged}"/>
Default Value:
If you set viewModel.Car = "VW", then it should auto-select that item in the combo box.
For this to work you will need to either implement INotifyPropertyChanged or set Car before you set DataContext.
INotifyPropertyChanged implementation might look like:
class ViewModel : INotifyPropertyChanged
{
private string car;
public ViewModel()
{
this.Car = "VW";
this.Cars = new ObservableCollection<string>() { "Mazda", "VW", "Audi" };
}
public string Car
{
get
{
return this.car;
}
set
{
if (this.car != value)
{
this.car = value;
OnPropertyChanged();
}
}
}
public ObservableCollection<string> Cars { get; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
2.
Bind ItemsSource and SelectedItem.
<ComboBox ItemsSource="{Binding Cars}"
SelectedItem="{Binding Car, Mode=TwoWay}">
</ComboBox>
You can also set ComboBox.ItemTemplate if your source or view is more complex than just displaying a string:
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ComboBox.ItemTemplate>
In the view model just add a list property:
public ObservableCollection<string> Cars { get; set; }
It doesn't have to be ObservableCollection but that type will auto-update the UI whenever you change the collection.
Update
Managed to fix the selectedIndex problem. I'd forgotten to set SelectedItem as well and naturally that caused a few issues.
So at 9AM this morning we got our 24 hour assignment and I have hit a brick wall.
We're supposed to create a program that allows a supervisor to Add and delete Employees and add Working Sessions, total hours and total earnings. But I am having some problems succesfully implementing this following the MVVM-Pattern. For some reason my Bindings simply aren't working and the only Solution I can see is someone looking over my project and helping me troubleshoot it.
Here is my code - I'm very sorry about having to post the entire thing but given that I have no clue where the problem is I did not see any other options. :
EmployeeModel
[Serializable]
public class WorkSessions : ObservableCollection<WorkSessionModel>
{
public WorkSessions()
{
}
}
[Serializable]
public class WorkSessionModel : INotifyPropertyChanged
{
private DateTime _dateTime;
private string _id;
private double _hours;
public WorkSessionModel()
{
}
public DateTime DateTime
{
get { return _dateTime; }
set
{
_dateTime = value;
NotifyPropertyChanged("DateTime");
}
}
public string ID
{
get { return _id; }
set
{
_id = value;
NotifyPropertyChanged("ID");
}
}
public double Hours
{
get { return _hours; }
set
{
_hours = value;
NotifyPropertyChanged("Hours");
NotifyPropertyChanged("TotalHours");
}
}
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
WorkSessionModel
[Serializable]
public class WorkSessions : ObservableCollection<WorkSessionModel>
{
public WorkSessions()
{
}
}
[Serializable]
public class WorkSessionModel : INotifyPropertyChanged
{
private DateTime _dateTime;
private string _id;
private double _hours;
public WorkSessionModel()
{
}
public DateTime DateTime
{
get { return _dateTime; }
set
{
_dateTime = value;
NotifyPropertyChanged("DateTime");
}
}
public string ID
{
get { return _id; }
set
{
_id = value;
NotifyPropertyChanged("ID");
}
}
public double Hours
{
get { return _hours; }
set
{
_hours = value;
NotifyPropertyChanged("Hours");
NotifyPropertyChanged("TotalHours");
}
}
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
EmployeeViewModel
public class EmployeeViewModel : ViewModelBase
{
private Employees _employeesModel = new Employees();
public Employees EmployeesView = new Employees();
public ObservableCollection<WorkSessionModel> WorkSessions { get; set; }
private string _id = "0";
private string _name = "noname";
private double _wage = 0;
private int _totalhours = 0;
public string ID
{
get { return _id; }
set { _id = value; RaisePropertyChanged("ID"); }
}
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
public double Wage
{
get { return _wage; }
set
{
_wage = value;
RaisePropertyChanged("Wage");
}
}
public int TotalHours
{
get { return _totalhours; }
set
{
_totalhours = value;
RaisePropertyChanged("TotalHours");
}
}
private EmployeeModel _selectedEmployee = new EmployeeModel();
public EmployeeModel SelectedEmployee
{
get { return _selectedEmployee; }
set
{
_selectedEmployee = value;
RaisePropertyChanged("SelectedEmployee");
}
}
private int _selectedEmployeeIndex;
public int SelectedEmployeeIndex
{
get { return _selectedEmployeeIndex; }
set
{
_selectedEmployeeIndex = value;
RaisePropertyChanged("SelectedEmployeeIndex");
}
}
#region RelayCommands
// Employee Relay Commands
public RelayCommand EmployeeAddNewCommand { set; get; }
public RelayCommand EmployeeDeleteCommand { set; get; }
public RelayCommand EmployeeNextCommand { set; get; }
public RelayCommand EmployeePrevCommand { set; get; }
public RelayCommand EmployeeTotalHoursCommand { get; set; }
#endregion
public EmployeeViewModel()
{
InitCommands();
}
private void InitCommands()
{
EmployeeAddNewCommand = new RelayCommand(EmployeeAddNewExecute, EmployeeAddNewCanExecute);
EmployeeDeleteCommand = new RelayCommand(EmployeeDeleteNewExecute, EmployeeDeleteCanExecute);
EmployeeNextCommand = new RelayCommand(EmployeeNextExecute, EmployeeNextCanExecute);
EmployeePrevCommand = new RelayCommand(EmployeePrevExecute, EmployeePrevCanExecute);
//EmployeeTotalHoursCommand = new RelayCommand(EmployeeTotalHoursExecute, EmployeeTotalHoursCanExecute);
}
//private void EmployeeTotalHoursExecute()
//{
// _selectedEmployee.TotalHours();
//}
//private bool EmployeeTotalHoursCanExecute()
//{
// return true;
//}
private void EmployeeAddNewExecute()
{
EmployeeModel newEmployee = new EmployeeModel();
EmployeesView.Add(newEmployee);
_employeesModel.Add(newEmployee);
SelectedEmployee = newEmployee;
}
private bool EmployeeAddNewCanExecute()
{
return true;
}
private void EmployeeDeleteNewExecute()
{
if (MessageBox.Show("You are about delete all submissions for Employee," + SelectedEmployee.Name + "(" + SelectedEmployee.ID +")\r\nAre you sure?", "This is a Warning!", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
_employeesModel.Remove(SelectedEmployee);
EmployeesView.Remove(SelectedEmployee);
}
}
private bool EmployeeDeleteCanExecute()
{
if (SelectedEmployee != null)
return true;
else return false;
}
private void EmployeeNextExecute()
{
SelectedEmployeeIndex++;
}
private bool EmployeeNextCanExecute()
{
if (SelectedEmployeeIndex < EmployeesView.Count - 1)
return true;
return false;
}
private void EmployeePrevExecute()
{
SelectedEmployeeIndex--;
}
private bool EmployeePrevCanExecute()
{
if (SelectedEmployeeIndex > 0)
return true;
return false;
}
}
View
public partial class MainWindow : Window
{
public EmployeeViewModel EmployeeViewModel = new EmployeeViewModel();
public MainWindow()
{
InitializeComponent();
menu_employee.DataContext = EmployeeViewModel;
sp_employees.DataContext = EmployeeViewModel;
datagrid_employees.ItemsSource = EmployeeViewModel.EmployeesView;
grid_selectedEmployee.DataContext = EmployeeViewModel.SelectedEmployee;
}
}
I can see a few problems with your code:
When the SelectedIndex is updated, SelectedItem remains the same and vice versa.
It looks like your data binding is out of order:
The DataContext property cascades down to every child of a certain dependency object.
The code in the MainWindow constructor should probably be replaced by:
this.DataContext = EmployeeViewModel;
Then in XAML set the rest of the properties using Data Binding. The problem in your situation is that the DataContext of the selectedemployee is only set once. This means if you select another employee, then it will not update.
An example for your SelectedEmployee grid:
<Grid Name="grid_selectedEmployee" DataContext="{Binding SelectedEmployee,
UpdateSourceTrigger=PropertyChanged}">...</Grid>
One of the biggest things I see is you are setting properties, not binding them.
For example,
datagrid_employees.ItemsSource = EmployeeViewModel.EmployeesView;
You are telling your DataGrid that it's ItemsSource should be that specific object. You need to bind it to that value so you are telling it to point to that property instead. This will make your UI correctly reflect what's in your ViewModel
The other huge red flag I see is your ViewModel referencing something called and EmployeeView which leads me to believe your View and ViewModel too closely tied together.
Your ViewModel should contain all your business logic and code, while the View is usually XAML and simply reflects the ViewModel in a user-friendly way.
The View and the ViewModel should never directly reference each other (I have had my View reference my ViewModel in some rare occasions, but never the other way around)
For example, an EmployeesViewModel might contain
ObservableCollection<Employee> Employees
Employee SelectedEmployee
ICommand AddEmployeeCommand
ICommand DeleteEmployeeCommand
while your View (XAML) might look like this:
<StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="Add" Command="{Binding AddEmployeeCommand}" />
<Button Content="Delete" Command="{Binding DeleteEmployeeCommand}" />
</StackPanel>
<DataGrid ItemsSource="{Binding Employees}"
SelectedItem="{Binding SelectedEmployee}">
... etc
</DataGrid>
<UniformGrid DataContext="{Binding SelectedEmployee}" Columns="2" Rows="4">
<TextBlock Text="ID" />
<TextBox Text="{Binding Id}" />
... etc
</UniformGrid >
</StackPanel>
And the only thing you should be setting is the DataContext of the entire Window. Usually I overwrite App.OnStartup() to start up my application:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var view = new MainWindow();
var vm = new EmployeesViewModel;
view.DataContext = vm;
view.Show();
}
}
Although I suppose in your case this would also work:
public MainWindow()
{
InitializeComponent();
this.DataContext = new EmployeesViewModel();
}