【发布时间】:2011-04-01 09:25:07
【问题描述】:
我正在尝试编写矩形网格,它确实会改变其对象的颜色。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 0; i < size; i++)
{
main_grid.ColumnDefinitions.Add(new ColumnDefinition());
main_grid.RowDefinitions.Add(new RowDefinition());
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cells[i, j] = new Cell { state = false, col = false };
Rectangle rect = new Rectangle();
Grid.SetColumn(rect, j);
Grid.SetRow(rect, i);
rect.Fill = Brushes.Orange;
rect.DataContext = cells[i, j];
rect.SetBinding(OpacityProperty, "ev_opacity");
Binding binding = new Binding("ev_col");
binding.Converter = new BooleanToBrushConverter();
rect.SetBinding(Rectangle.FillProperty, binding);
main_grid.Children.Add(rect);
}
}
setupTimer();
}
如何根据 col 设置矩形的颜色? (例如:真 - 黑,假 - 白)
单元类:
class Cell : INotifyPropertyChanged
{
private bool _state;
private bool _Col;
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler PropertyChanged2;
public bool Col; //to set color
{
get
{
return _Col;
}
set
{
_Col = value;
if (PropertyChanged2 != null)
{
PropertyChanged2(this, new PropertyChangedEventArgs("event2"));
};
}
}
public bool state //to set opacity
{
get
{
return _state;
}
set
{
_state = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ev_opacity"));
};
}
}
public static implicit operator int(Komorka c)
{
return Convert.ToInt32(c.state);
}
}
编辑: 这段代码不起作用 - 如果我点击网格,运行后什么也不会发生。
【问题讨论】:
标签: c# wpf events binding colors