wpf - Binding problem when creating custom ProgressBar -


<usercontrol x:class="wpfapplication2.progressbar"              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">     <grid>         <progressbar minimum="0" maximum="1"  value="0.5" largechange="0.1" smallchange="0.01" margin="2,2,12,2" height="22">             <progressbar.template>                 <controltemplate>                     <border borderthickness="2" borderbrush="black">                         <rectangle>                             <rectangle.fill>                                 <lineargradientbrush startpoint="0,0">                                     <lineargradientbrush.endpoint>                                         <point y="0" x="{binding relativesource={relativesource ancestortype={x:type progressbar}}, path=progressbar.value}"/>                                     </lineargradientbrush.endpoint>                                     <gradientstop color="transparent" offset="1.01"/>                                     <gradientstop color="#ff0000" offset="1.0"/>                                     <gradientstop color="#ffff00" offset="0.50"/>                                     <gradientstop color="#00ff00" offset="0.0"/>                                 </lineargradientbrush>                             </rectangle.fill>                         </rectangle>                     </border>                 </controltemplate>             </progressbar.template>         </progressbar>         <textblock text="50%" horizontalalignment="center" verticalalignment="center" />     </grid> </usercontrol> 

i error: "a 'binding' cannot set on 'x' property of type 'point'. 'binding' can set on dependencyproperty of dependencyobject."

  • is there clean workaround?

since point.x isn't dependency property can't bind something. bind endpointproperty though, , use converter creates point you. take y value parameter example

xaml

<lineargradientbrush.endpoint>     <binding relativesource="{relativesource ancestortype={x:type progressbar}}"              path="value"              converter="{staticresource pointxconverter}"              converterparameter="0"/> </lineargradientbrush.endpoint> 

pointxconverter

public class pointxconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         double progressbarvalue = (double)value;         double yvalue = system.convert.todouble(parameter);         return new point(progressbarvalue, yvalue);     }     public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         throw new notimplementedexception();     } } 

note: not related question if need bind y well, can use multibinding this

<lineargradientbrush.endpoint>     <multibinding converter="{staticresource pointconverter}">         <binding relativesource="{relativesource ancestortype={x:type progressbar}}"                  path="value"/>         <binding relativesource="{relativesource ancestortype={x:type progressbar}}"                  path="value"/>     </multibinding>                                         </lineargradientbrush.endpoint> 

pointconverter

public class pointconverter : imultivalueconverter {     public object convert(object[] values, type targettype, object parameter, system.globalization.cultureinfo culture)     {         double xvalue = (double)values[0];         double yvalue = (double)values[1];         return new point(xvalue, yvalue);     }     public object[] convertback(object value, type[] targettypes, object parameter, system.globalization.cultureinfo culture)     {         throw new notimplementedexception();     } } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -