c# - Access custom property inside ContentTemplate of custom user control -
i have following custom user control:
namespace myapp.controls { public partial class articlebutton: usercontrol { public articlebutton () { initializecomponent (); } public static readonly dependencyproperty titleproperty; static articlebutton () { titleproperty = dependencyproperty.registerattached ("title", typeof (string), typeof (articlebutton)); } [description ("the name of article."), category ("common properties")] public string title { { return "test"; } } } }
and corresponding xaml
:
<usercontrol x:class="myapp.controls.articlebutton" name="uc" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:myapp.controls"> <button name="button" click="button_click" style="{staticresource defaultbuttonstyle}"> <button.contenttemplate> <datatemplate> <grid> <textblock text="{binding path=title, elementname=uc}" /> </grid> </datatemplate> </button.contenttemplate> </button> </usercontrol>
where defaultbuttonstyle
defined in app.xaml
(there more that, should sufficient):
<style targettype="button" x:key="defaultbuttonstyle"> <setter property="template"> <setter.value> <controltemplate targettype="button"> <border name="border" borderthickness="1" padding="4,2" borderbrush="darkgray" cornerradius="3" background="{templatebinding background}"> <grid> <contentpresenter horizontalalignment="center" verticalalignment="center"/> </grid> </border> </controltemplate> </setter.value> </setter> </style>
my problem property title
not displayed, tried following did not work either:
<textblock text="{binding path=title, elementname=uc}" /> <textblock text="{binding path=title, relativesource={relativesource templatedparent}}" /> <textblock text="{binding path=title, relativesource={relativesource ancestortype={x:type local:articlebutton}}}" />
i found lots of posts similar issue, none of them helped... think problem try access custom property of custom user control inside content template of inner button, how that.
to sum comments 3rd relativesource
binding
<textblock text="relativesource={relativesource ancestortype={x:type local:articlebutton}}"/>
should work fine use register
instead of registerattached
. other bindings {binding path=title, elementname=uc}
won't work because conrtroltemplate
has own name scope , {binding path=title, relativesource={relativesource templatedparent}}"
won't work because don't set property against button
usercontrol
.
another problem set default value against clr wrapper and, mentioned on msdn page clr wrapper ignored , getvalue
/setvalue
methods called directly titleproperty
the wpf xaml processor uses property system methods dependency properties when loading binary xaml , processing attributes dependency properties. bypasses property wrappers. when implement custom dependency properties, must account behaviour , should avoid placing other code in property wrapper other property system methods getvalue , setvalue.
if want specify default value and/or property changed callback when create dependencyproperty
there variant of register
method takes propertymetadata
parameter can set these values.
another thing need aware when using dependency properties that, because definition static, default value shared between instances of articlebutton
. if type list or other class , initialize in propertymetadata
different null same instance shared default value
public partial class articlebutton : usercontrol { public articlebutton() { initializecomponent(); } public static readonly dependencyproperty titleproperty = dependencyproperty.register( "title", typeof(string), typeof(articlebutton), new propertymetadata("test", titlepropertychanged)); private static void titlepropertychanged(dependencyobject d, dependencypropertychangedeventargs e) { (d articlebutton).titlepropertychanged(e); } private void titlepropertychanged(dependencypropertychangedeventargs e) { //do when property changed value } public string title { { return (string)getvalue(titleproperty); } set { setvalue(titleproperty, value); } } }
Comments
Post a Comment