-
-
Save tymorrow/5c462b90b437d111eab0 to your computer and use it in GitHub Desktop.
Checkbox binding example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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