WPF: Binding to readonly property in code -
i'm doing rescaling on data in valueconverter whenever panel redrawn. want move of processing viewmodel of processing occurs if control size or few other properties change.
to ensure rescaled data looks acceptable need actualwidth
of container in viewmodel. want bind property of viewmodel 1 way when changes can trigger rescaling processing.
all examples find bind clr or dependency property element rather other way , i'm missing in understanding work out how should it. have have tried few different things setting binding not getting right.
any hints? thanks.
in myview xaml:
<myitemscontrol/>
in myview code behind, like:
binding b = new binding(mywidthproperty); b.mode = bindingmode.oneway; b.source = myitemscontrol.name; .........?
and
public static readonly dependencyproperty mywidthproperty = dependencyproperty.register( "mywidth", typeof(double), typeof(myviewmodel));
in myviewmodel:
public double mywidth{ { return _mywidth; } set { _mywidth = value; viewchanged(this); } }
you cannot way. cannot set binding
actualwidth
, it's read-only.
you can set binding mywidth
. this, need first convert mywidth dependencyproperty
. able like
binding b = new binding("actualwidth") { source = myitemscontrol }; this.setbinding(myviewmodel.mywidthproperty, b);
for converting dependency property, you'll need replace definition of mywidth
following:
public static readonly dependencyproperty mywidthproperty = dependencyproperty.register("mywidth", typeof(double), typeof(myviewmodel), new uipropertymetadata( 0.0, (d, e) => { var self = (myviewmodel)d; viewchanged(self); }));
but careful dependency properties; it's better read documentation first.
edit: need define property way:
public double mywidth { { return (double)this.getvalue(mywidthproperty); } set { this.setvalue(mywidthproperty, value); } }
Comments
Post a Comment