WPF矩形颜色结合 [英] WPF rectangle color binding
本文介绍了WPF矩形颜色结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写矩形网格至极确实改变了对象的颜色。
I'm trying to write grid of rectangles, wich does change color of its objects.
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:真 - 黑色,假的 - 白)
How to set color of rectangle in dependency with col? (f.e: true - black, false - white)
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);
}
}
编辑:
这code不工作 - 如果我对电网点击运行后没有发生
This code does not work - after run nothing happen if i click on grid.
推荐答案
绑定:
my_rect.SetBinding(Rectangle.OpacityProperty, "state_opacity");
my_rect.SetBinding(Rectangle.FillProperty,
new Binding()
{
Converter = new BooleanToBrushConverter(),
Path = new PropertyPath("state_color")
}
);
Cell类。长方形的颜色变化取决于state_color变量。
Cell class. Changing of colours of rectangles depends on "state_color" variable.
class Komorka : INotifyPropertyChanged
{
private bool _state_opacity;
private bool _state_color;
public event PropertyChangedEventHandler PropertyChanged;
public bool state_color
{
get { return _state_color; }
set
{
_state_color = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("state_color"));
};
}
}
public bool state_opacity
{
get
{
return _state_opacity;
}
set
{
_state_opacity = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("state_opacity"));
};
}
}
}
}
转换器类:
class BoolToBrush : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
if ((bool)value == false)
{
return new SolidColorBrush(Colors.Orange);
}
else
{
return new SolidColorBrush(Colors.Black);
}
}
else
{
return new SolidColorBrush(Colors.Red);
}
}
这篇关于WPF矩形颜色结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文