使用IValueConverter更改按钮颜色(代码隐藏)

时间:2014-02-11 01:46:29

标签: c# wpf binding converter

我目前有一个对象(Cell)设置为按钮的数据上下文。按钮的颜色必须依赖于属于该对象的属性(CellState);如果单元格是ALIVE,则它所绑定的Button的background属性设置为Black,如果单元格为DEAD,则Button为White。我必须使用值转换器来处理单元格“状态”的可视化表示,所以我有一个如下所示的ValueConverter:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        SolidColorBrush b = null;
        State s = (State)value;
        if (s == State.ALIVE)
        {
            b = Brushes.Black;
        }
        else if (s == State.DEAD)
        {
            b = Brushes.White;
        }

        return b;
    }

我不确定如何使用值转换器将按钮的background属性正确绑定到Cell的CellState属性。这就是我到目前为止所做的:

Button b = new Button();
b.DataContext = new Cell();
b.Background = Brushes.White;
b.Click += StateChangeClick_Handler;

Binding binding = new Binding();

StateToBrushConverter stateConverter = new StateToBrushConverter();
binding.Converter = stateConverter;
Cell c = (Cell)b.DataContext;

binding.ConverterParameter = c.CellState;
b.SetBinding(Button.BackgroundProperty, binding);

cellGrid.Children.Add(b);

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

以下是一个有效示例的完整源代码 - 您基本上需要确保在其属性绑定到按钮的自定义类上实现INotifyPropertyChanged。在我的示例中,我创建了一个简单的应用程序,其中包含一个按钮(与您已有的相同)和一个复选框,因此我可以通过编程方式更改Cell对象的State属性,并查看背景更改。

所以这是:

public partial class MainWindow : Window
{
    private Cell cell = new Cell();
    public MainWindow()
    {
        InitializeComponent();
        b.DataContext = cell;
        b.Background = Brushes.White;

        Binding binding = new Binding("CellState");
        StateToBrushConverter stateConverter = new StateToBrushConverter();
        binding.Converter = stateConverter;
        Cell c = (Cell)b.DataContext;

        binding.ConverterParameter = c.CellState;
        b.SetBinding(Button.BackgroundProperty, binding);
    }

    private void checkBox1_Checked(object sender, RoutedEventArgs e)
    {
        this.cell.CellState = (bool) checkBox1.IsChecked ? State.ALIVE : State.DEAD;
    }
}

public enum State { ALIVE, DEAD }

public class Cell : INotifyPropertyChanged
{
    private State state;
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public State CellState
    {
        get { return this.state; }
        set
        {
            if (value != this.state)
            {
                this.state = value;
                NotifyPropertyChanged("CellState");
            }
        }
    }
}

[ValueConversion(typeof(State), typeof(String))]
public class StateToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        SolidColorBrush b = null;
        State s = (State)value;
        if (s == State.ALIVE)
        {
            b = Brushes.Black;
        }
        else if (s == State.DEAD)
        {
            b = Brushes.White;
        }

        return b;
    }


    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
相关问题