Tuesday, 15 September 2015

wpf - How is TemplateBinding working in UserControl Template? -



wpf - How is TemplateBinding working in UserControl Template? -

i new 1 create usercontrol , trying customize usercontrol template below:

<usercontrol x:class="wpfapplication1.piebutton" 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" mc:ignorable="d" d:designheight="300" d:designwidth="300" loaded="onloaded"> <usercontrol.template> <controltemplate> <path name="path" stroke="aqua" strokethickness="3"> <path.fill> <solidcolorbrush color="{templatebinding fill}" /> </path.fill> <path.data> ...... </usercontrol>

at same time have create dependencyproperty in back-end code:

public partial class piebutton : usercontrol { public piebutton() { initializecomponent(); } private void onloaded(object sender, routedeventargs e) { } public color fill { { homecoming (color)getvalue(fillproperty); } set { setvalue(fillproperty, value); } } public static readonly dependencyproperty fillproperty = dependencyproperty.register("fill", typeof(color), typeof(piebutton)); ......

i want utilize templatebinding in xaml bind fill property of piebutton fill path object. visual studio designer warns me "the fill property not accessible or recognized".

based on understanding, templatebinding find property name element apply controltemplate, should piecontrol here, why fill property cannot access here?

btw,

i test next binding, , can work me

color="binding fill,relativesource={relativesource mode=findancestor,ancestortype={x:type usercontrol}}"

but still think templatebinding should able work under scenario, please point out fault here. thanks.

according templatebinding dependencyproperty on custom command not working, templatebinding not work custom dependency properties on controls.

as solution suggested utilize

{binding myproperty, relativesource={relativesource templatedparent}}

wpf xaml user-controls controltemplate templatebinding

No comments:

Post a Comment