How to bind command to a button I explained here.
Here is my XAML:
<Window x:Class="UpdateBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="clr-namespace:UpdateBinding.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewModel:UpdateBindingViewModel x:Key="UpdateBindingBindingViewModel" />
</Window.Resources>
<Grid DataContext="{StaticResource UpdateBindingBindingViewModel}">
<StackPanel>
<TextBox Name="MyTextBox" Text="{Binding Ubm.Name}" VerticalAlignment="Top"/>
<Button VerticalAlignment="Top" Command="{Binding OnButtonClick}">test</Button>
</StackPanel>
</Grid>
</Window>
Command to update value looks like this:
public void UpdateBinding(object obj)
{
Ubm.Name = "Updated value";
OnPropertyChanged(() => this.Ubm);
}
Part of code:
protected void OnPropertyChanged<T>(Expression<Func<T>> property)
{
if (this.PropertyChanged != null)
{
var mex = property.Body as MemberExpression;
string name = mex.Member.Name;
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
I took from here.
Also don't forget to implement INotifyPropertyChanged interface, in my case declaration of viewmodel looks like this:
public class UpdateBindingViewModel: INotifyPropertyChanged
Example application you can download from here.
---
2014-06-11 Update Just a short notice. If you implement interface by default (with resharper alt + enter :) ) then to use it, you have to write which property of class (view model) are you updating. Something like:
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
if (propertyName == "test")
{
MyHidden = !MyHidden;
myContent = "Clicked!";
}
}
}
Where OnPropertyChanged is called like this:
OnPropertyChanged("MyHidden");
Obviously this example is not compatible with previous example :) I hope I will update this article soon...
---
2014-06-13 Another update
Check out following code:
namespace BooleanToVisibilityConverterExample.ViewModel
{
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
using BooleanToVisibilityConverterExample.Annotations;
public class BooleanToVisibilityConverterExampleViewModel: INotifyPropertyChanged
{
public ICommand OnClick { get; set; }
public bool MyHidden { get; set; }
public BooleanToVisibilityConverterExampleViewModel()
{
OnClick = new RelayCommand(ShowHide);
MyHidden = true;
}
private void ShowHide(object obj)
{
MyHidden = !MyHidden;
OnPropertyChanged("MyHidden");
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Here notice lines:
MyHidden = !MyHidden;
OnPropertyChanged("MyHidden");
With these lines I said that I want to update property MyHidden which is bounded to rectangle visibility. This update actually is coming from this article, and example you can download from here.