wpf - Binding GradientStop works but reports error -
the following code binds gradientstop
background.color
property of templatedparent
. works getting binding error in output window:
system.windows.data error: 2 : cannot find governing frameworkelement or frameworkcontentelement target element. bindingexpression:path=background.color; dataitem=null; target element 'gradientstop' (hashcode=6944299); target property 'color' (type 'color')
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:class="wpfbindingtest.mainwindow" x:name="window" title="mainwindow" width="100" height="100"> <window.resources> <controltemplate x:key="gradienttemplate" targettype="{x:type contentcontrol}"> <border borderthickness="1" borderbrush="{templatebinding background}"> <border.background> <lineargradientbrush endpoint="0.5,1" startpoint="0.5,0"> <gradientstop color="{binding path=background.color, relativesource={relativesource templatedparent}}" offset="1"/> <gradientstop color="white" offset="0"/> </lineargradientbrush> </border.background> <contentpresenter/> </border> </controltemplate> </window.resources> <grid x:name="layoutroot"> <contentcontrol background="green" template="{staticresource gradienttemplate}" > <textblock verticalalignment="center" horizontalalignment="center" text="x" /> </contentcontrol> </grid> </window>
i had same error in visual studio console output.
a possible explanation , workaround reported here
basically if use converter returns lineargradientbrush don't error
the code this
[valueconversion(typeof(system.windows.media.color), typeof(lineargradientbrush))] class gradientconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { var brush = new lineargradientbrush(); var color = (color)value; brush.startpoint = new point(0.5, 0); brush.endpoint = new point(0.5, 1); brush.gradientstops.add(new gradientstop(colors.white, 0)); brush.gradientstops.add(new gradientstop((color)value, 1)); return brush; } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
and in xaml
<border borderthickness="1" borderbrush="{templatebinding background}" background="{binding path=background.color, relativesource={relativesource templatedparent}, converter={staticresource gradconv}}">
Comments
Post a Comment