c# - DependencyProperty compiles and works but Intellisense doesn't think so -
i have 2 custom controls/visuals , need orientation
property on both. in both cases, default should "horizontal"
user of control/visual should able specify orientation="vertical"
arrange components of control/visual vertically. have works great on imagebutton control, not on headeredlabel visual. although both of them compile fine, intellisense doesn't 1 of them. here's example of use...
<visuals:imagebutton image="icons/ok.png" content="normal content"/> <visuals:imagebutton image="icons/ok.png" content="vertical content" orientation="vertical"/> <visuals:headeredlabel header="normal header" content="normal content"/> <visuals:headeredlabel header="vertical header" content="vertical content" orientation="vertical"/>
...which produces following when rendered inside vertical stackpanel:
so want, problem this: while intellisense recognizes possible options orientation
imagebutton, it not recognize possible options for orientation
for headeredlabel. , while code compiles & runs fine, there's persistent error in visual studio "error list" pane: "'vertical' not valid value property 'orientation'.", and there's blue squiggly line under text orientation="vertical"
second headeredlabel in xaml example above.
here relevant files:
// file 'imagebutton.cs' using system.windows; using system.windows.controls; using system.windows.media; namespace visuals { public class imagebutton : button { static imagebutton() { defaultstylekeyproperty.overridemetadata(typeof(imagebutton), new frameworkpropertymetadata(typeof(imagebutton))); } public imagesource image { { return (imagesource)getvalue(imageproperty); } set { setvalue(imageproperty, value); } } public orientation orientation { { return (orientation)getvalue(orientationproperty); } set { setvalue(orientationproperty, value); } } // note imagebutton, can 'orientation.horizontal' , // compiler resolves system.windows.controls.orientation... public static readonly dependencyproperty orientationproperty = dependencyproperty.register("orientation", typeof(orientation), typeof(imagebutton), new uipropertymetadata(orientation.horizontal)); public static readonly dependencyproperty imageproperty = dependencyproperty.register("image", typeof(imagesource), typeof(imagebutton), new uipropertymetadata(null)); } }
.
// file 'headeredlabel.cs' using system.windows; using system.windows.controls; using system.windows.media; namespace visuals { public class headeredlabel : control { static headeredlabel() { defaultstylekeyproperty.overridemetadata(typeof(headeredlabel), new frameworkpropertymetadata(typeof(headeredlabel))); } public object header { { return (object)getvalue(headerproperty); } set { setvalue(headerproperty, value); } } public object content { { return (object)getvalue(contentproperty); } set { setvalue(contentproperty, value); } } public object orientation { { return (object)getvalue(orientationproperty); } set { setvalue(orientationproperty, value); } } public static readonly dependencyproperty headerproperty = dependencyproperty.register("header", typeof(object), typeof(headeredlabel), new uipropertymetadata(null)); public static readonly dependencyproperty contentproperty = dependencyproperty.register("content", typeof(object), typeof(headeredlabel), new uipropertymetadata(null)); // note headeredlabel, unlike imagebutton, have specify fully- // qualified name of 'orientation.horizontal', otherwise compiler resolves // visuals.headeredlabel.orientation , gives compiler error... public static readonly dependencyproperty orientationproperty = dependencyproperty.register("orientation", typeof(system.windows.controls.orientation), typeof(headeredlabel), new uipropertymetadata(system.windows.controls.orientation.horizontal)); } }
.
<!-- file 'generic.xaml' --> <resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:visuals" xmlns:bind="clr-namespace:visuals.bindingconverters"> <style targettype="{x:type local:imagebutton}"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type local:imagebutton}"> <button> <stackpanel orientation="{templatebinding orientation}"> <image source="{templatebinding image}"/> <contentpresenter/> </stackpanel> </button> </controltemplate> </setter.value> </setter> </style> <style targettype="{x:type local:headeredlabel}"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type local:headeredlabel}"> <stackpanel orientation="{templatebinding orientation}"> <stackpanel orientation="horizontal"> <contentcontrol content="{templatebinding header}" /> <textblock text=":" /> </stackpanel> <contentcontrol content="{templatebinding content}"/> </stackpanel> </controltemplate> </setter.value> </setter> </style> </resourcedictionary>
any ideas why compiler resolve orientation.horizontal
system.windows.controls.orientation.horizontal imagebutton, not headeredlabel? , more importantly, ideas why intellisense can't figure out options headeredlabel.orientation?
btw, i'm using visualstudio 2012 , .net framework 4.0.
all of properties, including orientation
property, declared having type object
.
you should have instead:
public orientation orientation { { return (orientation)getvalue(orientationproperty); } set { setvalue(orientationproperty, value); } }
the xaml editor should able correctly accept values of type orientation
if declare property correctly. otherwise, attempt assign string
value of "vertical"
property, when passed setvalue()
method fail, because dependencyproperty
object initialized orientation
valid type , has no way convert string
value orientation
value.
if declare property correctly, wpf understand automatically needs convert string
value shown in xaml orientation
value property (i.e. parse string
value appropriate enum
type), , in case initialization should work.
Comments
Post a Comment