c# - Change Image object Visibility -



c# - Change Image object Visibility -

i have image in project. want alter property visibility true when button pressed. created image

<image name="userimgrock" source="rock.png" horizontalalignment="left" height="100" margin="277" verticalalignment="top" width="100" visibility="hidden"/>

and button

private void btnrock_click(object sender, routedeventargs e) { userimgrock.visibility = visibility.visible; }

but there error error *the name 'userimgrock' not exist in current context. i'm little confused. help!

if these both xaml , code behind belongs same class prefix name x: x: referring xaml name space, in window tag xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

eg

<image x:name="userimgrock" source="rock.png" horizontalalignment="left" height="100" margin="277" verticalalignment="top" width="100" visibility="hidden"/>

if these both not in same class may not able utilize event handler accessing objects in other class objects private respective class.

edit

after looking @ code discovered few things including reason why cannot access image

first things first, why cannot access image object?

reason is, you've defined image within command template limits scope of objects within template itself, not accessible outside

how fix?

you can define property need access in code behind , bind them respective properties in xaml

eg

define dependency property userimgrockvisibility in usercontrol

public visibility userimgrockvisibility { { homecoming (visibility)getvalue(userimgrockvisibilityproperty); } set { setvalue(userimgrockvisibilityproperty, value); } } // using dependencyproperty backing store userimgrockvisibility. enables animation, styling, binding, etc... public static readonly dependencyproperty userimgrockvisibilityproperty = dependencyproperty.register("userimgrockvisibility", typeof(visibility), typeof(usercontrol1), new propertymetadata(visibility.hidden));

set info context in constructor

datacontext = this;

or in xaml via binding self

<usercontrol x:class="rockpaper.usercontrol1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"... datacontext="{binding relativesource={relativesource self}}">

then utilize property manipulate visibility

userimgrockvisibility = visibility.visible;

finally bind property visibility of image in xaml

<image x:name="userimgrock" visibility="{binding userimgrockvisibility}" ... />

name not necessary if not need it

this need in order command property of object within command template or info template

another approach

since not find significance of using style , command template within user control

you can removing style , command template elements , bring elements usercontrol

by doing may not require properties may access objects in class scope

all can see you've used them mouse on detection, can utilize event triggers in elements straight using mouseenter, mouseleave kind of events

eg,

<usercontrol x:class="rockpaper.usercontrol1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <grid> <button x:name="btnrock" opacity=".3" horizontalalignment="left" margin="44,82.418,0,0" verticalalignment="top" width="100" height="100" click="btnrock_click"> <stackpanel> <image /> </stackpanel> </button> <image x:name="userimgrock" visibility="hidden" horizontalalignment="left" height="100" margin="277.164,228.418,0,0" verticalalignment="top" width="100" opacity="1" /> </grid> <usercontrol.triggers> <eventtrigger sourcename="btnrock" routedevent="mouseenter" > <beginstoryboard> <storyboard> <doubleanimation x:name="enter" to="1" storyboard.targetname="btnrock" storyboard.targetproperty="opacity"/> </storyboard> </beginstoryboard> </eventtrigger> <eventtrigger sourcename="btnrock" routedevent="mouseleave" > <beginstoryboard> <storyboard> <doubleanimation to=".3" storyboard.targetname="btnrock" storyboard.targetproperty="opacity"/> </storyboard> </beginstoryboard> </eventtrigger> </usercontrol.triggers> </usercontrol>

note stripped of code not necessary example

then utilize image object as

userimgrock.visibility = visibility.visible;

now selection yours take prefer. seek both , see convenient you.

c# wpf xaml visual-studio-2012

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -