c# - How do I read an Image's Height and Width from my View Model? -
i want line-drawing operations viewmodel (i.e. add collection of lines bound itemscontrol
on view).
for need know height , width of image
in view. how them ?
there workarounds mentioned in other posts they wpf , none of them work on metro.
public static class sizeobserver { public static readonly dependencyproperty observeproperty = dependencyproperty.registerattached( "observe", typeof(bool), typeof(sizeobserver), new propertymetadata(null, onobservechanged)); //new frameworkpropertymetadata(onobservechanged)); public static readonly dependencyproperty observedwidthproperty = dependencyproperty.registerattached( "observedwidth", typeof(double), typeof(sizeobserver), null); public static readonly dependencyproperty observedheightproperty = dependencyproperty.registerattached( "observedheight", typeof(double), typeof(sizeobserver), null); public static bool getobserve(frameworkelement frameworkelement) { //frameworkelement.assertnotnull("frameworkelement"); return (bool)frameworkelement.getvalue(observeproperty); } public static void setobserve(frameworkelement frameworkelement, bool observe) { //frameworkelement.assertnotnull("frameworkelement"); frameworkelement.setvalue(observeproperty, observe); } public static double getobservedwidth(frameworkelement frameworkelement) { //frameworkelement.assertnotnull("frameworkelement"); return (double)frameworkelement.getvalue(observedwidthproperty); } public static void setobservedwidth(frameworkelement frameworkelement, double observedwidth) { //frameworkelement.assertnotnull("frameworkelement"); frameworkelement.setvalue(observedwidthproperty, observedwidth); } public static double getobservedheight(frameworkelement frameworkelement) { //frameworkelement.assertnotnull("frameworkelement"); return (double)frameworkelement.getvalue(observedheightproperty); } public static void setobservedheight(frameworkelement frameworkelement, double observedheight) { //frameworkelement.assertnotnull("frameworkelement"); frameworkelement.setvalue(observedheightproperty, observedheight); } private static void onobservechanged(dependencyobject dependencyobject, dependencypropertychangedeventargs e) { var frameworkelement = (frameworkelement)dependencyobject; if ((bool)e.newvalue) { frameworkelement.sizechanged += onframeworkelementsizechanged; updateobservedsizesforframeworkelement(frameworkelement); } else { frameworkelement.sizechanged -= onframeworkelementsizechanged; } } private static void onframeworkelementsizechanged(object sender, sizechangedeventargs e) { updateobservedsizesforframeworkelement((frameworkelement)sender); } private static void updateobservedsizesforframeworkelement(frameworkelement frameworkelement) { // wpf 4.0 onwards frameworkelement.setvalue(observedwidthproperty, frameworkelement.actualwidth); frameworkelement.setvalue(observedheightproperty, frameworkelement.actualheight); // wpf 3.5 , prior ////setobservedwidth(frameworkelement, frameworkelement.actualwidth); ////setobservedheight(frameworkelement, frameworkelement.actualheight); } }
Comments
Post a Comment