Skip to content

Instantly share code, notes, and snippets.

@tymorrow
Last active August 29, 2015 14:04
Show Gist options
  • Save tymorrow/5c462b90b437d111eab0 to your computer and use it in GitHub Desktop.
Save tymorrow/5c462b90b437d111eab0 to your computer and use it in GitHub Desktop.
Checkbox binding example
<Window x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:testApp="clr-namespace:TestApp"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<testApp:MainWindowViewModel />
</Window.DataContext>
<Grid>
<StackPanel>
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
<TextBox Text="{Binding Text}" />
</StackPanel>
</Grid>
</Window>
namespace TestApp
{
using System.ComponentModel;
public class MainWindowViewModel : INotifyPropertyChanged
{
private bool _isChecked;
private string _text;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
if (_isChecked)
{
Text = "Hello!";
}
else if (!_isChecked)
{
Text = string.Empty;
}
RaisePropertyChanged("IsChecked");
}
}
public string Text
{
get { return _text; }
set { _text = value; RaisePropertyChanged("Text"); }
}
public MainWindowViewModel()
{
}
// Property Changed Event Handlers
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment