i need answer can explore. running brick wall here:
i use:
(int c = 0; c < 255; c++) { color = color.fromargb(255, (byte)c, (byte)(255 - c), (byte)c); thread.sleep(3); }
each time "color" recomputed, window or canvas in window change color...so see cool color changes. why hard? need see this, i'm hitting brick wall. copied type converter here:
[valueconversion(typeof(color),typeof(solidcolorbrush))] public class colorbrushconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { color color = (color)value; return new solidcolorbrush(color); } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { return null; } }
thank help!
a simple example of how can bind color property code behind canvas background. things note
- implement inotifypropertychanged. background canvas updated upon onpropertychanged execute everytime mycolorproperty changes.
- setting datacontext = this; in constructor make controls in window inherit datacontext (unless overriden).
xaml
<canvas ...> <canvas.background> <solidcolorbrush color="{binding mycolorproperty}"/> </canvas.background>
code behind
public partial class mainwindow : window, inotifypropertychanged { public mainwindow() { initializecomponent(); this.datacontext = this; } // ... private color m_mycolorproperty; public color mycolorproperty { { return m_mycolorproperty; } set { m_mycolorproperty = value; onpropertychanged("mycolorproperty"); } } public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } private void switchbackground() { if (mycolorproperty == colors.red) { mycolorproperty = colors.black; } else { mycolorproperty = colors.red; } } }
update
run background animation can use backgroundworker this
private backgroundworker m_changecolorbgworker = null; public mainwindow() { initializecomponent(); this.datacontext = this; m_changecolorbgworker = new backgroundworker(); m_changecolorbgworker.dowork += new doworkeventhandler(m_changecolorbgworker_dowork); } private void button1_click(object sender, routedeventargs e) { if (m_changecolorbgworker.isbusy == false) { m_changecolorbgworker.runworkerasync(); } } void m_changecolorbgworker_dowork(object sender, doworkeventargs e) { while (true) { (int c = 0; c < 254; c++) { mycolorproperty = color.fromargb(255, (byte)c, (byte)(255 - c), (byte)c); thread.sleep(10); } } }
you can set mycolorproperty directly in thread because changes fired inotifypropertychanged automatically marshalled onto dispatcher. not go inotifycollectionchanged however, know.
Comments
Post a Comment