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"
xmlns:local="clr-namespace:WpfApplication1"
>
<Window.DataContext>
<local:MyViewModel/>
</Window.DataContext>
<Window.Resources>
<local:BooleanToTextConverter x:Key="booleanToTextConverter" />
<local:BoolToColorConverter x:Key="booleanToColorConverter" />
</Window.Resources>
<Grid>
<StackPanel Background="{Binding Path=IsChecked, Converter={StaticResource booleanToColorConverter}}">
<CheckBox IsChecked="{Binding Path=IsChecked}" Grid.Column="0">Check Me!</CheckBox>
<TextBlock FontSize="30" HorizontalAlignment="Center" Text="{Binding IsChecked, Converter={StaticResource booleanToTextConverter}}" />
</StackPanel>
</Grid>
C#: ViewModel:
public class MyViewModel : INotifyPropertyChanged
{
bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set {
_isChecked = value;
OnPropertyChanged("IsChecked");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
Конвертеры:
public class BooleanToTextConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value ? "Est Bon Client" : "Est Mauvais Client";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class BoolToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value ? new SolidColorBrush(Colors.GreenYellow) : new SolidColorBrush(Colors.DarkRed);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Возможно, попробуйте документацию: https://msdn.microsoft.com/en-us/library/ee230084(v=vs.110).aspx –
Связывание и обложка - вот что вам нужно. Сообщение «un peu de code», если вам нужен пример – adPartage
' ' Его хороший способ сделать это? – Memo