【问题标题】:WPF boolean column checkboxWPF 布尔列复选框
【发布时间】:2015-05-21 22:05:27
【问题描述】:

我想将 Datagrid 中“EstBonClient”单元格的布尔复选框更改为特定颜色。如果选中复选框,则单元格的背景颜色将为绿色,如果未选中复选框,则单元格的背景颜色将为红色。我希望复选框不在 Datagrid 中。

谢谢你们!

【问题讨论】:

  • Binding 和 Coverter 是您所需要的。如果您需要示例,请发布un peu de code
  • <Style TargetType="{x:Type CheckBox}"> <Style.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter Property="Background" Value="LightGreen"/> </Trigger> </Style.Triggers> </Style> 做事的好方法?
  • 发布您的 Datagrid 的 XAML。

标签: c# wpf checkbox datagrid


【解决方案1】:

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();
    }
}

【讨论】:

  • 感谢您的回答。 &lt;DataGrid x:Name="DataGrid1" AllowDrop="False" IsReadOnly="True" ItemsSource="{Binding ListeClient}"&gt; &lt;/DataGrid&gt; 我的 DataGrid 就是这样。我怎样才能实现这个转换器?谢谢
  • 我等待你的答复
  • 放在边界之间&lt;Border Background="{Binding Path=IsChecked, Converter={StaticResource booleanToColorConverter}}"&gt; &lt;DataGrid x:Name="DataGrid1" AllowDrop="False" IsReadOnly="True" ItemsSource="{Binding ListeClient}"&gt; &lt;/DataGrid&gt; &lt;/Border&gt;
猜你喜欢
  • 2015-04-07
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 2016-01-13
  • 2011-10-14
相关资源
最近更新 更多