我試圖寫矩形的網格,它確實會改變其對象的顏色。WPF矩形顏色綁定
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();
}
如何根據列設置矩形的顏色? (f.e:真 - 黑,假 - 白)
Cell類:
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);
}
}
編輯: 此代碼不能正常工作 - 後運行什麼,如果我對電網點擊發生。
在這個答案中不太清楚你是如何解決點擊事件的。 – 2011-04-03 12:32:03