Reactive UI alternative to Parameter for CanExecute (WPF) - wpf

Let's say I have two VM classes ParentVM and ChildVM. ParentVM has ObservableCollection<ChildVM> Children. Also ParentVM has two commands MoveUpCommand and MoveDownCommand which implemented by MoveUp(ChildVM child){..} and MoveDown(ChildVM child){...}
In my view I have a ListBox which ItemSource binds to Children collection. ItemTemplate contains TextBlock and two buttons (Move Up and Move Down) for each child. I bind commands of this buttons to Parent commands and use ListBoxItem's DataContext i.e. ChildVM as CommandParameter.
So far so good it works)
Problem is there when I want to set proper CanExecute method. Because I don't want to Button be active when Child cannot be moved up (i.e. already on top). And there is a catch, when I write implementation of my command I have my ChildVM as parameter so I can check whether it on top or at the bottom and simply ignore MoveUp or MoveDown. But there is nothing like that for CanExecute.
I read some question, someone advice to bind parameter to some property. But how? I think I can create int CollectionIndex property (which I will update from Parent) in ChildVM and transfer commands to ChildVM, but it looks like something that I should not do.
Any common solution here?
UPDATE 1
Some code for demonstation. This code is simplified to make it shorter.
public class ParentVM
{
public ObservableCollection<ChildVM> Children { get; }
public ICommand MoveUpCommand { get; }
public ParentVM()
{
Children = new ObservableCollection<ChildVM>();
Children.Add(new ChildVM { Name = "Child1" });
Children.Add(new ChildVM { Name = "Child2" });
Children.Add(new ChildVM { Name = "Child3" });
MoveUpCommand = ReactiveCommand.Create<ChildVM>(MoveUp);
}
public void MoveUp(ChildVM child)
{
var index = Children.IndexOf(child);
if (index > 0) Children.Move(index, index - 1);
}
}
public class ChildVM
{
public string Name { get; set; }
}
ParentView.xaml
<UserControl ...>
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Children}" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Width="*"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.MoveUpCommand}"
CommandParameter="{Binding}">Move Up</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
ParentView.xaml.cs
public partial class ParentView : UserControl
{
public ParentView()
{
InitializeComponent();
this.DataContext = new ParentVM();
}
}
I write it again here. 'Move Up' button appears FOR EACH child. In this case SelectedItem cannot be used because I can just press button without actually selecting item

There are two answers below. The first uses standard MVVM patterns and is very simple. However, it doesn't use Reactive UI at all. The second does use ReactiveCommand from Reactive UI, but frankly it's messy. I'm not an expert on Reactive UI, but I think this is one case where it's quite hard to write a decent answer using it. Maybe a Reactive UI expert will see this and correct me.
For the first solution I just took the RelayCommand class from Josh Smith's orginal MVVM paper. We can then rewrite your class very slightly and everything works. The XAML is exactly as in the question. You asked for what the 'common solution' would be and I expect this is it.
public class ParentVM
{
public ObservableCollection<ChildVM> Children { get; }
public ICommand MoveUpCommand { get; }
public ParentVM()
{
Children = new ObservableCollection<ChildVM>();
Children.Add(new ChildVM { Name = "Child1" });
Children.Add(new ChildVM { Name = "Child2" });
Children.Add(new ChildVM { Name = "Child3" });
MoveUpCommand = new RelayCommand(o => MoveUp((ChildVM)o), o => CanExecuteMoveUp((ChildVM)o));
}
public void MoveUp(ChildVM child)
{
var index = Children.IndexOf(child);
if (index > 0) Children.Move(index, index - 1);
}
public bool CanExecuteMoveUp(ChildVM child)
{
return Children.IndexOf(child) > 0;
}
}
public class ChildVM
{
public string Name { get; set; }
}
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute; _canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter) { _execute(parameter); }
}
Of course the solution above uses an extra class, RelayCommand, and doesn't use the standard ReactiveCommand. The Reactive UI docs say 'Parameters, unlike in other frameworks, are typically not used in the canExecute conditions, instead, binding View properties to ViewModel properties and then using the WhenAnyValue() is far more common.'. However this is quite tricky to do in this case.
The second solution below uses ReactiveCommand. It binds the commands into the child VMs, which call the parent VM when the command is executed. The child VMs know their index position in the list, so we can base canExecute on this. As you can see the parents and children end up tightly-coupled and this isn't pretty.
public class ParentVM
{
public ObservableCollection<ChildVM> Children { get; }
public ParentVM()
{
Children = new ObservableCollection<ChildVM>();
Children.Add(new ChildVM { Name = "Child1", Parent=this, Index = 0 });
Children.Add(new ChildVM { Name = "Child2", Parent = this, Index = 1 });
Children.Add(new ChildVM { Name = "Child3", Parent = this, Index = 2 });
}
public void MoveUp(ChildVM child)
{
var index = Children.IndexOf(child);
if (index > 0)
{
Children.Move(index, index - 1);
Children[index].Index = index;
Children[index - 1].Index = index - 1;
}
}
}
public class ChildVM: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Name { get; set; }
public ParentVM Parent { get; set; }
public ICommand MoveUpCommand { get; }
private int _index;
public int Index
{
get { return _index; }
set { _index = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Index))); }
}
public ChildVM()
{
IObservable<bool> canExecute = this.WhenAnyValue(x => x.Index, index => index > 0);
MoveUpCommand = ReactiveCommand.Create(() => Parent.MoveUp(this), canExecute);
}
}
XAML:
<UserControl>
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Children}" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Width="*"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding MoveUpCommand}">Move Up</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>

Related

TreeView MVVM, how to get selection?

I am trying to create a TreeView for my application. This is the first time I am using TreeView with the MVVM structure, by all accounts the binding is working and displaying correctly.
However:
How do I get the selection so I can perform some logic after the user selects something?
I thought that the TextValue property in SubSection class would fire PropertyChanged, but it doesn't, so I am left scratching my head.
This is the most simplified set of code I could make for this question:
Using PropertyChanged setup like this in : ViewModelBase class
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
The VeiwModel:
public class ShiftManagerViewModel : ViewModelBase
{
public ShiftManagerViewModel()
{
Departments = new List<Department>()
{
new Section("Section One"),
new Section("Section Two")
};
}
private List<Section> _sections;
public List<Section> Sections
{
get{return _sections;}
set
{
_sections = value;
NotifyPropertyChanged();
}
}
}
The classes:
public class Section : ViewModelBase
{
public Section(string depname)
{
DepartmentName = depname;
Courses = new List<SubSection>()
{
new SubSection("SubSection One"),
new SubSection("SubSection One")
};
}
private List<SubSection> _courses;
public List<SubSection> Courses
{
get{ return _courses; }
set
{
_courses = value;
NotifyPropertyChanged();
}
}
public string DepartmentName { get; set; }
}
public class SubSection : ViewModelBase
{
public SubSection(string coursename)
{
CourseName = coursename;
}
public string CourseName { get; set; }
private string _vTextValue;
public string TextValue
{
get { return _vTextValue; }
set
{
_vTextValue = value;
NotifyPropertyChanged();
}
}
}
And the XAML:
<Window.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Courses}" DataType="{x:Type viewModels:Section}">
<Label Content="{Binding DepartmentName}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding TextValue}" DataType="{x:Type viewModels:SubSection}">
<Label Content="{Binding CourseName}" />
</HierarchicalDataTemplate>
</Window.Resources>
Could someone point me in the right direction?
You could cast the SelectedItem property of the TreeView to a Section or a SubSection or whatever the type of the selected item is:
Section section = treeView1.SelectedItem as Section;
if (section != null)
{
//A Section is selected. Access any of its properties here
string name = section.DepartmentName;
}
else
{
SubSection ss = treeView1.SelectedItem as SubSection;
if(ss != null)
{
string ssName = ss.CourseName;
}
}
Or you could add an IsSelected property to the Section and SubSection types and bind the IsSelected property of the TreeView to it:
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
Then you get the selected item by iterating through the ItemsSource of the TreeView and look for the item that has the IsSelected source property set to true.

How to display values of a bound datagrid

I have a WPF application and I'm usign the MVVM design approach.
I have an ObservableObject which holds a list of the class Transaction which is below;
public Transaction
{
public string Ammount;
public string Direction;
}
The ObservableObject in my ViewModel is below
public ObservableCollection<Transactions> ListOfUserTransactions
{
get { return _localTransactionData; }
set
{
_localTransactionData = value;
RaisePropertyChanged("ListOfUserTransactions");
}
}
And my view binds in this way;
<DataGrid ItemsSource="{Binding ListOfUserTransactions}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Ammount" Binding="{Binding Path=Ammount}" Foreground="Black" IsReadOnly="True" />
<DataGridTextColumn Header="Direction" Binding="{Binding Path=Direction}" Foreground="Black" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
This does work in a way as the number of rows produced equal what i was expecting but all the rows are empty, i presume it is the way i am binding the DataGridTextColumn but im unsure as how to do it, hope there is enough detail in the above question.
EDIT
below is how the transaction list is created
ObservableCollection<Transaction> _localTransactionData = new ObservableCollection<Transaction>();
public TransactionsViewModel(User passedThroughUser)
{
_user = passedThroughUser;
_listOfTransactions = _user.Transactions;
foreach (var item in _listOfTransactions)
{
LocalTransactionsList tran = new LocalTransactionsList();
tran.Ammount = item.Ammount.ToString(); ;
tran.Direction = item.Direction;
_localTransactionData.Add(tran);
}
}
Your problem is here:
public Transaction
{
public string Ammount;
public string Direction;
}
should be:
public Transaction
{
public string Ammount {get;set;}
public string Direction {get;set;}
}
Binding properties must have the setter and getter.
You can get the behaviour you are after by changing your Transaction class to this...
public class Transaction : INotifyPropertyChanged
{
private string _amount;
public string Amount
{
[DebuggerStepThrough]
get { return _amount; }
[DebuggerStepThrough]
set
{
if (value != _amount)
{
_amount = value;
OnPropertyChanged("Amount");
}
}
}
private string _direction;
public string Direction
{
[DebuggerStepThrough]
get { return _direction; }
[DebuggerStepThrough]
set
{
if (value != _direction)
{
_direction = value;
OnPropertyChanged("Direction");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string name)
{
var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
This will enable the WPF binding engine to access your properties and bind them correctly.
More about INPC here http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

ObservableCollection don't modify view

I have strange WPF ObservableCollection behavior. By unclear reason when collection modified and there is another condition in getter-method in some property of my class, it does't modify View. Although CollectionChanged event was invoked!
Without condition in getter method collection works good.
Too complicated and long-winded to explain here what I do in my work project. Therefore I make small simplify project and emulate same behavior. This project show you problem better then thousand words.
To see the problem - launch project as it is, looks how it works. It is really simple, 2 radiobuttons, datagrid and nothing else. Then go to the MainWindowViewModel, GridItems-property, uncomment commented code and launch project again. See the difference. When collection modify, get-method of GridItems-property dont't invoke (I check it with debugger). How not invoked method can make affect on something??? I don't have any idea on it. Help plz.
Project link:
http://www.megafileupload.com/en/file/443850/ObservableCollection-zip.html
class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<GridItem> _totalStorage;
private ObservableCollection<GridItem> _gridItems;
public ObservableCollection<GridItem> GridItems
{
get
{
//if (_gridItems.Count == 0)
//{
// return _totalStorage;
//}
return _gridItems;
}
set
{
_gridItems = value;
RaisePropertyChanged("GridItems");
}
}
public MainWindowViewModel()
{
_totalStorage = new ObservableCollection<GridItem>();
_gridItems = new ObservableCollection<GridItem>();
GridItemsInit();
_gridItems.CollectionChanged += CollectionChanged;
}
/// <summary>
/// Collection change event handler
/// </summary>
/// <param name="o"></param>
/// <param name="e"></param>
private void CollectionChanged(object o, NotifyCollectionChangedEventArgs e)
{
}
private void GridItemsInit()
{
_totalStorage.Add(new GridItem
{
Name = "Igor",
LastName = "Balachtin",
FilerField = FileStatusEnum.All
});
_totalStorage.Add(new GridItem
{
Name = "Misha",
LastName = "Ivanov",
FilerField = FileStatusEnum.All
});
_totalStorage.Add(new GridItem
{
Name = "Ahmed",
LastName = "Magamed",
FilerField = FileStatusEnum.All
});
_totalStorage.Add(new GridItem
{
Name = "abrek",
LastName = "cheburek",
FilerField = FileStatusEnum.All
});
_totalStorage.Add(new GridItem
{
Name = "Irka",
LastName = "Dirka",
FilerField = FileStatusEnum.All
});
}
private void RefreshGridSource(string statusParam)
{
_gridItems.Clear();
//Если нажали на баттон new
if (statusParam.Equals(FileStatusEnum.All.ToString()))
{
foreach (var item in _totalStorage)
{
_gridItems.Add(item);
}
}
//Если нажали на archived
else if (statusParam.Equals(FileStatusEnum.Filtered.ToString()))
{
foreach (var item in _totalStorage.Where(g => g.FilerField == FileStatusEnum.Filtered))
{
_gridItems.Add(item);
}
}
}
private RelayCommand<object> _radioCommand;
public RelayCommand<object> RadioCommand
{
get { return _radioCommand ?? (_radioCommand = new RelayCommand<object>(HandlerFileRadio)); }
}
private void HandlerFileRadio(object obj)
{
if (obj == null)
return;
var statusParam = obj.ToString();
RefreshGridSource(statusParam);
}
}
Look at this sample.
//if (_gridItems.Count == 0)
//{
// return _totalStorage;
//}
Model:
public enum FileStatusEnum
{
All = 0,
Filtered
}
public class GridItem
{
public String Name { get; set; }
public String LastName { get; set; }
public FileStatusEnum FilerField
{
get; set;
}
}
Xaml:
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<RadioButton Margin="5" IsChecked="True" Command="{Binding RadioCommand}"
CommandParameter="All">All</RadioButton>
<RadioButton Margin="5" Command="{Binding RadioCommand}"
CommandParameter="Filtered">Filtered</RadioButton>
</StackPanel>
<DataGrid Grid.Column="1" ItemsSource="{Binding GridItems}" CanUserAddRows="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True" />
<DataGridTextColumn Header="LastName" Binding="{Binding LastName}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
</Grid>
really better post all code here instead of link? :/
Once you are changing record after that your property is not Notifying.so please Notify after changing the collection.
Add below line in RefreshGridSource method after collection changed .
RaisePropertyChanged("GridItems");
Your GridItems property can return either _totalStorage or _gridItems depending upon a condition; _totalStorage and _gridSettings are two different instances of ObservableCollection. Initially, when _gridItems has no items, your GridItems property returns _totalStorage to WPF, and WPF listens on this instance for any CollectionChanged events.
In your RefreshGridSource method you are updating _gridItems (a differnt instance from _totalStorage), which WPF has no knowledge of.
But, when you rasie property changed for GridItems property from RefreshGridSource method WPF will re-read the property GridItems - this time, WPF gets _gridItems collection and it works as you expected.
Hope, I have explained well enough.

How to enable/disable a button in WPF?

I've got a relatively simple WPF application. The idea is to present a list of items to the user; for each item there is a checkbox to select/deselect the item.
My code, simplified, looks a bit like this:
class Thing { /* ... */ };
public static int SelectedCount { get; set; }
public class SelectableThing
{
private bool _selected;
public bool Selected {
get { return _selected; }
set { _selected = value; if (value) { SelectedCount++; } else { SelectedCount--; } }
}
public Thing thing { get; set; }
};
private ObservableCollection<SelectableThing> _selectableThings;
public Collection<SelectableThing> { get { return _selectableThings; } }
<DataGrid ItemSource="{Binding Path=SelectableThings}">
<DataGridCheckBoxColumn Binding="{Binding Selected}"/>
<DataGridTextColumn Binding="{Binding Thing.name}"/>
</DataGrid>
<Button Content="{Binding Path=SelectedTestCount}" Click="someFunc" />
So the idea is that the button content should show the count of tests selected. This should be accomplished because whenever SelectableThing.Selected is set, it should increment/decrement the SelectedCount as appropriate.
However, as far as I can tell the behavior doesn't work. The button text displays "0", regardless of selecting/deselecting items in the list.
Any ideas?
This problem is a little hairy since you have multiple view-model classes involved. Here's a crack at the code to solve this. The only thing I'm missing is that the DataGrid doesn't seem to update your items until you leave the row.
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding Path=SelectableThings}"
Grid.Row="0" Margin="6">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="IsSelected"
Binding="{Binding IsSelected}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
<Button Content="{Binding Path=SelectedTestCount}"
Command="{Binding SaveCommand}"
Grid.Row="1" Width="75" Height="23"
HorizontalAlignment="Right" Margin="0,0,6,6"/>
</Grid>
</Window>
Thing class:
public class Thing : INotifyPropertyChanged
{
private readonly List<SelectableThing> selectableThings;
private DelegateCommand saveCommand;
public Thing(IEnumerable<SelectableThing> selectableThings)
{
this.selectableThings = new List<SelectableThing>(selectableThings);
this.SelectableThings =
new ObservableCollection<SelectableThing>(this.selectableThings);
this.SaveCommand = this.saveCommand = new DelegateCommand(
o => Save(),
o => SelectableThings.Any(t => t.IsSelected)
);
// Bind children to change event
foreach (var selectableThing in this.selectableThings)
{
selectableThing.PropertyChanged += SelectableThingChanged;
}
SelectableThings.CollectionChanged += SelectableThingsChanged;
}
public ObservableCollection<SelectableThing> SelectableThings
{
get;
private set;
}
public int SelectedTestCount
{
get { return SelectableThings.Where(t => t.IsSelected).Count(); }
}
public ICommand SaveCommand { get; private set; }
private void Save()
{
// Todo: Implement
}
private void SelectableThingChanged(object sender,
PropertyChangedEventArgs args)
{
if (args.PropertyName == "IsSelected")
{
RaisePropertyChanged("SelectedTestCount");
saveCommand.RaiseCanExecuteChanged();
}
}
private void SelectableThingsChanged(object sender,
NotifyCollectionChangedEventArgs e)
{
foreach (SelectableThing selectableThing in
e.OldItems ?? new List<SelectableThing>())
{
selectableThing.PropertyChanged -= SelectableThingChanged;
RaisePropertyChanged("SelectedTestCount");
}
foreach (SelectableThing selectableThing in
e.NewItems ?? new List<SelectableThing>())
{
selectableThing.PropertyChanged += SelectableThingChanged;
RaisePropertyChanged("SelectedTestCount");
}
}
public void RaisePropertyChanged(string propertyName)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
SelectableThing class:
public class SelectableThing : INotifyPropertyChanged
{
private string name;
private bool isSelected;
public SelectableThing(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged("Name");
}
}
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
RaisePropertyChanged("IsSelected");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Original Answer:
Bind Command to an ICommand. Set your CanExecute on the ICommand to return false when your condition isn't satisified.
In the setter for that IsSelected property, when the value changes, raise the CanExecuteChanged event.
The Command binding on a Button automatically enables/disables the button based on the result of CanExecute.
For more information on how to do this, including an implementation of ICommand that you could use here, see this mini-MVVM tutorial I wrote up for another question.
To fill out the implementaiton of CanExecute, I'd use something like Linq's .Any method. Then you don't have to bother checking Count, and can terminate the loop early if you find that any item is checked.
For example:
this.SaveCommand = new DelegateCommand(Save, CanSave);
// ...
private void Save(object unusedArg)
{
// Todo: Implement
}
private bool CanSave(object unusedArg)
{
return SelectableThings.Any(t => t.IsSelected);
}
Or since it is short, use a lambda inline:
this.SaveCommand = new DelegateCommand(Save,
o => SelectableThings.Any(t => t.IsSelected)
);
Bind the Content of the button to a field in your viewmodel and fire the OnChanged method for that field every time another item is selected or unselected. Bind the IsEnabled to a boolean field in your view model and set it to true/false as appropriate to enable or disable the button.

How can I update a property of mainWindowViewModel from another ViewModel?

From mainWIndow.xaml, which uses as DataContext the mainWindowViewModel, I opening a new window with name addNewItem.xaml, which uses as DataContext the ItemsViewModel.
In addNewItem.xaml I have a DataGrid
<DataGrid SelectedItem="{Binding Path=SelectedHotel}" ItemsSource="{Binding HotelsList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Width="350" Header="Hotel" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=Name}"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I want to pass the SelectedHotel from ItemsViewModel to mainWindowViewModel.
I tried to do this with the following code (with no luck)
//This is a property from ItemsViewModel
private Hotel _selectedHotel { get; set; }
public Hotel SelectedHotel {
get { return _selectedHotel; }
set {
_selectedHotel = value;
RaisePropertyChanged("SelectedHotel");
OnSelectedItemChanged();
}
}
void OnSelectedItemChanged() {
MainWIndowViewModel = new MainWIndowViewModel(this.SelectedHotel);
}
In mainWIndowViewModel I have also one more Property (with same name, SelectedHotel) which it gets value through the constructor
public MainWIndowViewModel(Hotel selectedHotel)
: this(new ModalDialogService()) {
this.SelectedHotel = selectedHotel;
}
In mainWindow.xaml I want to display a value of the property
<Label Content="{Binding SelectedHotel.Name}" DockPanel.Dock="Top"></Label>
what am I doing wrong?
In general, I need to know the rule of doing something like this.
How could I notify a property from another property?
Thanks
Solution
I solve it with messages from mvvm light.
Finally I found a solution and this comes from mediator pattern. I use the mvvmLight.
From mainWindowViewModel, I registered a message (I don't know if the term of message is the correct one)
public MainWIndowViewModel(IDialogService dialog) {
this._dialog = dialog;
Messenger.Default.Register<NotificationMessage<Hotel>>(this, NotificationMessageReceived);
}
private void NotificationMessageReceived(NotificationMessage<Hotel> selectedHotel) {
this.SelectedHotel = selectedHotel.Content;
}
from the other viewModel, I send a message with the SelectedHotel.
private Hotel _selectedHotel { get; set; }
public Hotel SelectedHotel {
get { return _selectedHotel; }
set {
_selectedHotel = value;
RaisePropertyChanged("SelectedHotel");
Messenger.Default.Send(new NotificationMessage<Hotel>(this, SelectedHotel, "SelectedHotel"));
}
}
In the ItemsViewModel you do not need the OnSelecetedItemChanged handler code at all. Instead, in the MainWindowViewModel store a reference to the ItemsViewModel, and add a handler there:
var ItemsViewModel = itemsVM = new ItemsViewModel();
itemsVm.PropertyChanged += SelectedHotelChanged;
// and then implement the handler:
public void SelectedHotelChanged(object sender, PropertyChangedEventArgs args)
{
SelectedHotel = itemsVM.SelectedHotel;
}
// ensuring that the SelectedHotel property in the MainWindowViewModel also notifies when it changes.
Edit: Fixed a typo.

Resources