wpf - UserControl enum DependencyProperty not binding -
i created usercontrol contains 3 dependencyproperties. 2 working fine, there 1 gives me real headache. have enum (outside usercontrol class same namespace):
public enum recordingtype { norecording, continuesrecording, eventrecording }
i created dependencyproperty follows:
public static dependencyproperty selectedrecordingtypeproperty = dependencyproperty.register("selectedrecordingtype", typeof(recordingtype), typeof(schedulercontrol), new frameworkpropertymetadata((recordingtype)recordingtype.norecording, frameworkpropertymetadataoptions.bindstwowaybydefault)); public recordingtype selectedrecordingtype { { return (recordingtype)getvalue(selectedrecordingtypeproperty); } set { setvalue(selectedrecordingtypeproperty, value); } }
and i'm using in xaml this:
<usercontrols:schedulercontrol grid.row="1" grid.column="3" selectedrecordingtype="{binding currentrecordingtype,updatesourcetrigger=propertychanged,mode=twoway}" fullrecordingschedule="{binding mondayfullrecordingschedule,updatesourcetrigger=propertychanged}" selectedrecordingtime="{binding mondayselectedrecordingtime,updatesourcetrigger=propertychanged}"/>
there 2 more dependencyproperties work fine (i get , set methods inside usercontrol), 1 no-go. created dps before , i'm doing same. made sure binding in vm ok , getter , setter being called correctly.
any great!
also checked vm. binding execute.
let me show other solution usercontrol
(uc now) combobox
, enum
bindings. common problem, when can bind enum, can't selecteditem
of combobox
uc. solution provide selecteditem
.
for example, have exampleuc : usercontrol
uc class, able accept enum, , provide selecteditem
. using properties (attributes in .xaml). have enum, called exampleenum
, , window
, creates new instance of exampleuc
, setting that's properties/attributes.
in exampleuc.xaml:
<usercontrol x:class="testnamespace.view.exampleuc" xmlns:view="clr-namespace:testnamespace.view" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:markup="http://schemas.microsoft.com/winfx/2006/xaml"> <grid datacontext="{binding relativesource={relativesource ancestortype={markup:type view:exampleuc}}}"> <combobox itemssource="{binding enumtypearray}" selecteditem="{binding selecteditem}"/> </grid> </usercontrol>
as can see, datacontext
of uc has been set it's ancestor's datacontext, means can receive wanted parameters (you can find more explanations datacontext inheritance , visual tree, make researches them).
the binded properties (enumtypearray , selecteditem) dependencyproperties in exampleuc.xaml.cs file:
public array enumtypearray { { return (array)getvalue(enumtypearrayproperty); } set { setvalue(enumtypearrayproperty, value); } } public object selecteditem { { return (object)getvalue(selecteditemproperty); } set { setvalue(selecteditemproperty, value); } } public static readonly dependencyproperty enumtypearrayproperty = dependencyproperty.register("enumtypearray", typeof(array), typeof(exampleuc), new propertymetadata(new string[0])); public static readonly dependencyproperty selecteditemproperty = dependencyproperty.register("selecteditem", typeof(object), typeof(exampleuc), new propertymetadata(null));
to create new dependencyproperty
can use propdp
code snippet. (write , press tab default). these properties shown attributes in .xaml editor, when create , edit instances of exampleuc.
at stage have uc, can accept enum, , return selecteditem
.
the enum somewhere:
public enum exampleenum { example1, example2 }
the window
, uses exampleuc:
you have add new resource window's resources, objectdataprovider
in order able use enum itemssource
:
<window.resources> <objectdataprovider x:key="myenumname" methodname="getvalues" objecttype="{x:type sys:enum}"> <objectdataprovider.methodparameters> <x:type typename="local:exampleenum"/> </objectdataprovider.methodparameters> </objectdataprovider> </window.resources>
please note that, local
namespace prefix has been defined earlier @ namespaces' section, namespace of exampleenum
, example:
xmlns:local="clr-namespace:testnamespace.data"
to use exampleuc
, in grid
or panel
, use following:
<views:exampleuc enumtypearray="{binding source={staticresource myenumname}}" selecteditem="{binding myproperty, mode=twoway}"/>
to set mode
twoway
necessary able get
, set
property. please note that, might have define views
namespace @ namespaces' section, if visual studio wouldn't you.
as can see, defined dependencyproperties showing attributes. enumtypearray
responsible fill combobox's items, , selecteditem
has been binded myproperty, property in model class, such as:
public exampleenum myproperty{ get{ return _myproperty;} set{ _myproperty = value; onpropertychanged("myproperty"); } }
this example shows how use enums through ucs. since uc has single component (a combobox
), it's useless in practice. if decorate label
or others, job.
hope helps.
Comments
Post a Comment