i've got 4 sliders set values of rgba channels of color
using converter shown below:
<slider width="200" x:name="redslider" minimum="0.0" maximum="1.0" value="{binding newcolor, converter={staticresource colorchannelconverter}, converterparameter ='r'}"/>
the converter code:
public class colorchannelconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { var c = (color)value; var p = (string)parameter; double channel = 0.0; switch(p) { case "r": channel = c.r; break; case "g": channel = c.g; break; case "b": channel = c.b; break; case "a": channel = c.a; break; } return channel / 255.0; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { // i'd like: // var d = (byte)((double)value*255); // if((string)parameter == "r") newcolor.r = d; // item other channels // return newcolor; // note don't want touch newcolor's other channels! throw new notimplementedexception(); } }
now acquiring channel simple can see. however, converter doesn't have access newcolor
property can set individual channels. how can go doing without using control collect values of 4 sliders?
instead of binding entire color, want bind single component of color, such mycolor.r
example. problem built in color
struct not observable, view not update appropriately.
the way solved same issue in color picker application working on creating custom class represent color (which called observablecolor
) implements inotifypropertychanged
, has observable properties individual color channels. internally, uses system.windows.media.color
storage , exposes property other parts of application need display color.
here example of mean:
class observablecolor : inotifypropertychanged { private color mcolor; public color color { { return mcolor; } set { // update mcolor , fire property change notifications "color" // , of individual channel properties } } public byte { { return mcolor.a; } set { // update mcolor.a , fire property change notifications "color" , "a" } } public byte r { // same a, r color channel } // etc. }
using allow bind newcolor.r
instead of newcolor
. way converter needs return value channel instead of returning whole color (which has no way of knowing, found).
Comments
Post a Comment