user controls - WPF UserControl creating multiple layout choices -


i have usercontrol have various properties defined, can customise each copy that's on screen. have path uses lineargradientbrush fill. @ present hard coded xaml. have width , visibility of path control dependency objects , can modify these:

<path      visibility="{templatebinding pathavisibility}"     width="{templatebinding pathalength}">         <lineargradientbrush endpoint="0,0.5" mappingmode="relativetoboundingbox" startpoint="1,0.5">             <gradientstop color="#07ffffff" offset="0.812"/>             <gradientstop color="red"/>             <gradientstop color="#00000000" offset="0.993"/>             <gradientstop color="#ff956666" offset="0.62"/>         </lineargradientbrush>... 

what i'd create few gradients options can select properties in wpf xaml designer. "grada" red, "gradb" blue, doesn't have transparency, etc.

with visibility can see "visible/hidden/collapsed" options choose in design view, , kind of thing i'm after.

this i'm stuck. don't know called, or how approach it.

any pointers on direction should looking?

you can use enum provide fixed values want in xaml, can use propertychangedcallback on enum dependencyproperty change brush.

here very quick example.

code:

public partial class usercontrol1 : usercontrol {     public usercontrol1()     {         initializecomponent();         datacontext = this;     }      public brushtype brushtype     {         { return (brushtype)getvalue(brushtypeproperty); }         set { setvalue(brushtypeproperty, value); }     }      // using dependencyproperty backing store brushtype.  enables animation, styling, binding, etc...     public static readonly dependencyproperty brushtypeproperty =         dependencyproperty.register("brushtype", typeof(brushtype), typeof(usercontrol1)         , new propertymetadata(brushtype.none, new propertychangedcallback(onbrushtypechanged)));      private static void onbrushtypechanged(dependencyobject d, dependencypropertychangedeventargs e)     {         var usercontrol = d usercontrol1;         if (e.newvalue brushtype)         {             usercontrol.mybrush = usercontrol.findresource(e.newvalue.tostring()) brush;         }     }      public brush mybrush     {         { return (brush)getvalue(mybrushproperty); }         set { setvalue(mybrushproperty, value); }     }      // using dependencyproperty backing store mybrush.  enables animation, styling, binding, etc...     public static readonly dependencyproperty mybrushproperty =         dependencyproperty.register("mybrush", typeof(brush), typeof(usercontrol1), new propertymetadata(null));  }  public enum brushtype {     none,     brusha,     brushb,     brushc } 

xaml:

<usercontrol x:class="wpflistboxgrouptest.usercontrol1"              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">     <usercontrol.resources>         <solidcolorbrush x:key="brusha" color="red" />         <solidcolorbrush x:key="brushb" color="yellow" />         <solidcolorbrush x:key="brushc" color="blue" />     </usercontrol.resources>      <grid background="{binding mybrush}" />  </usercontrol> 

usage:

<stackpanel orientation="horizontal">     <local:usercontrol1 brushtype="brusha" />     <local:usercontrol1 brushtype="brushb" />     <local:usercontrol1 brushtype="brushc" /> </stackpanel> 

result:

enter image description here


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -