WPF 布尔列复选框
WPF boolean column checkbox
我想将我的 Datagrid 中 "EstBonClient" 单元格的布尔复选框更改为特定颜色。如果复选框被选中,单元格的背景颜色将为绿色,如果复选框未被选中,单元格的背景颜色将为红色。我希望该复选框不在 Datagrid 中。
谢谢你们!
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#:
视图模型:
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();
}
}
我想将我的 Datagrid 中 "EstBonClient" 单元格的布尔复选框更改为特定颜色。如果复选框被选中,单元格的背景颜色将为绿色,如果复选框未被选中,单元格的背景颜色将为红色。我希望该复选框不在 Datagrid 中。
谢谢你们!
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#: 视图模型:
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();
}
}