DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > An Exploration of the Silverlight Toolbar Control

An Exploration of the Silverlight Toolbar Control

Sony Arouje user avatar by
Sony Arouje
·
May. 08, 12 · Mobile Zone · Interview
Like (0)
Save
Tweet
4.83K Views

Join the DZone community and get the full member experience.

Join For Free

in this post i am explaining how to create a basic toolbar in silverlight. this toolbar has two parts

    • toolbar panel
    • toolbar item

screen shot

image

toolbar panel

this user control holds all the toolbar items. this control internally uses stackpanel to organize the toolbar items. below is the xaml and code behind

xaml

<usercontrol x:class="controls.toolbarpanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:ignorable="d"
             background="transparent"
    d:designheight="300" d:designwidth="400">
    
    <grid x:name="layoutroot" background="transparent">
        <stackpanel x:name="itemholder" orientation="{binding orientation}" background="transparent"></stackpanel>
    </grid>
</usercontrol>

code behind

using system;
using system.collections.generic;
using system.linq;
using system.net;
using system.windows;
using system.windows.controls;
using system.windows.documents;
using system.windows.input;
using system.windows.media;
using system.windows.media.animation;
using system.windows.shapes;

namespace controls
{
    public partial class toolbarpanel : usercontrol
    {
        public event click toolbaritemclick;
        public toolbarpanel()
        {
            initializecomponent();
            this.loaded += new routedeventhandler(toolbarpanel_loaded);
        }

        void toolbarpanel_loaded(object sender, routedeventargs e)
        {
            this.createclickevent();
        }

        public static dependencyproperty toolbaritemsproperty = dependencyproperty.register("toolbaritems",
        typeof(presentationframeworkcollection<toolbaritem>),
        typeof(toolbarpanel),
        new propertymetadata(null));
        public uielementcollection toolbaritems
        {
            get 
            {
                return itemholder.children;
            }
        }

        public static dependencyproperty toolbaritemsorientation = dependencyproperty.register("orientation",
        typeof(orientation),
        typeof(toolbarpanel),
        new propertymetadata(null));
        public orientation orientation
        {
            get { return (itemholder.orientation); }
            set { itemholder.orientation=value; }
        }
        public override void onapplytemplate()
        {
            base.onapplytemplate();
            itemholder.datacontext = this;
        }

        private void createclickevent()
        {
            foreach (uielement elem in itemholder.children)
            {
                toolbaritem item = elem as toolbaritem;
                if(item!=null)
                    item.toolbaritemclick += new click(item_toolbaritemclick);
            }
        }

        void item_toolbaritemclick(object sender, string key)
        {
            if (toolbaritemclick != null)
                this.toolbaritemclick(sender, ((toolbaritem)sender).toolbaritemkey);
        }

    }
}

one property i wanted to highlight here is the toolbaritems dependency property. this property exposes the stackpanels children property. users can add toolbar items through this property. at the end of this post, with a sample code i will explain the properties.

the delegate click used in the toolbarpanel is declared in toolbaritem code.

toolbar item

this control creates the toolbar button, it inherits from button control. to make it similar to toolbar button i modified the resource template of the button. apart from the default properties of button a new property is added to provide the ability to add image.

xaml

<button x:class="controls.toolbaritem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:ignorable="d"
    d:designheight="300" d:designwidth="400">
    <button.resources>
        <controltemplate x:key="btntoolbaritemstyle" targettype="button">
            <grid>
                <visualstatemanager.visualstategroups>
                    <visualstategroup x:name="commonstates">
                        <visualstate x:name="disabled"/>
                        <visualstate x:name="normal"/>
                        <visualstate x:name="mouseover">
                            <storyboard>
                                <doubleanimation duration="0" to="1" storyboard.targetproperty="(uielement.opacity)" storyboard.targetname="border" d:isoptimized="true"/>
                                <objectanimationusingkeyframes storyboard.targetproperty="(frameworkelement.margin)" storyboard.targetname="border">
                                    <discreteobjectkeyframe keytime="0">
                                        <discreteobjectkeyframe.value>
                                            <thickness>0</thickness>
                                        </discreteobjectkeyframe.value>
                                    </discreteobjectkeyframe>
                                </objectanimationusingkeyframes>
                                <objectanimationusingkeyframes storyboard.targetproperty="(border.borderthickness)" storyboard.targetname="border">
                                    <discreteobjectkeyframe keytime="0">
                                        <discreteobjectkeyframe.value>
                                            <thickness>1</thickness>
                                        </discreteobjectkeyframe.value>
                                    </discreteobjectkeyframe>
                                </objectanimationusingkeyframes>
                                <coloranimation duration="0" to="#fff9b167" storyboard.targetproperty="(border.background).(gradientbrush.gradientstops)[1].(gradientstop.color)" storyboard.targetname="border" d:isoptimized="true"/>
                                <coloranimation duration="0" to="#ffffefb6" storyboard.targetproperty="(border.background).(gradientbrush.gradientstops)[0].(gradientstop.color)" storyboard.targetname="border" d:isoptimized="true"/>
                                <objectanimationusingkeyframes storyboard.targetproperty="(border.cornerradius)" storyboard.targetname="border">
                                    <discreteobjectkeyframe keytime="0">
                                        <discreteobjectkeyframe.value>
                                            <cornerradius>4</cornerradius>
                                        </discreteobjectkeyframe.value>
                                    </discreteobjectkeyframe>
                                </objectanimationusingkeyframes>
                                <coloranimation duration="0" to="#ffdea158" storyboard.targetproperty="(border.borderbrush).(solidcolorbrush.color)" storyboard.targetname="border" d:isoptimized="true"/>
                            </storyboard>
                        </visualstate>
                        <visualstate x:name="pressed"/>
                    </visualstategroup>
                </visualstatemanager.visualstategroups>
                <border x:name="border" borderbrush="black" borderthickness="1" margin="1,2,2,1" opacity="0">
                    <border.background>
                        <lineargradientbrush endpoint="0.5,1" startpoint="0.5,0">
                            <gradientstop color="black" offset="0"/>
                            <gradientstop color="white" offset="1"/>
                        </lineargradientbrush>
                    </border.background>
                </border>
                <stackpanel orientation="horizontal">
                    <border x:name="imgborder"  borderbrush="black" borderthickness="0" margin="0" width="16" height="16">
                        <border.background>
                            <imagebrush x:name="imgborderbrush" stretch="fill" imagesource="{binding toolbaritemimage}"/>
                        </border.background>
                    </border>
                    <textblock x:name="caption" grid.column="1" textwrapping="nowrap" text="{binding content}" fontsize="9.333" textalignment="center" verticalalignment="center" />
                </stackpanel>
            </grid>
        </controltemplate>
    </button.resources>

    <button.template>
        <staticresource resourcekey="btntoolbaritemstyle"/>
    </button.template>
    
</button>

 

code behind

using system;
using system.collections.generic;
using system.linq;
using system.net;
using system.windows;
using system.windows.controls;
using system.windows.documents;
using system.windows.input;
using system.windows.media;
using system.windows.media.animation;
using system.windows.shapes;
using system.collections;
using system.componentmodel;
namespace controls
{
    public delegate void click(object sender, string key);
    public partial class toolbaritem : button
    {
        public event click toolbaritemclick;
        public toolbaritem()
        {
            initializecomponent();
            this.click += new routedeventhandler(toolbaritem_click);
        }

        void toolbaritem_click(object sender, routedeventargs e)
        {
            if (toolbaritemclick != null)
                this.toolbaritemclick(sender, (string)getvalue(toolbaritemkeyproperty));
        }

        public static dependencyproperty toolbaritemimageproperty = dependencyproperty.register("toolbaritemimage",
        typeof(imagesource),
        typeof(toolbaritem),
        new propertymetadata(null));
        public imagesource toolbaritemimage
        {
            get { return (imagesource)getvalue(toolbaritemimageproperty); }
            set { setvalue(toolbaritemimageproperty, value); }
        }

        public static dependencyproperty toolbaritemkeyproperty = dependencyproperty.register("toolbaritemkey",
        typeof(string),
        typeof(toolbaritem),
        new propertymetadata(null));
        public string toolbaritemkey
        {
            get { return (string)getvalue(toolbaritemkeyproperty); }
            set { setvalue(toolbaritemkeyproperty, value); }
        }

        public override void onapplytemplate()
        {
            base.onapplytemplate();
            setdatacontext();
        }
        private void setdatacontext()
        {
            border border = gettemplatechild("imgborder") as border;
            border.datacontext = this;
            textblock txtcaption = gettemplatechild("caption") as textblock;
            txtcaption.datacontext = this;

            if (this.content == null)
            {
                txtcaption.margin = new thickness(0);
                border.margin = new thickness(3);
            }
            else
            {
                txtcaption.margin = new thickness(3);
            }
        }

    }
}
  • toolbaritemimage: to set the source of the image to display in the toolbar.

  • toolbaritemkey: provide a key to identify which button is clicked.

test page

the test page helps you to understand how we can use this new control

xaml

<controls:childwindow x:class="popups.testtoolbar"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:controls="clr-namespace:system.windows.controls;assembly=system.windows.controls"
           xmlns:tlbar="clr-namespace:emblemdesigner.controls" title="testtoolbar" mc:ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:designheight="265" d:designwidth="432">
    <grid x:name="layoutroot" margin="2">
        <grid.rowdefinitions>
            <rowdefinition />
            <rowdefinition height="auto" />
        </grid.rowdefinitions>
        

        <tlbar:toolbarpanel height="50" background="transparent" orientation="horizontal" toolbaritemclick="toolbarpanelext_toolbaritemclick" >
            <tlbar:toolbarpanel.toolbaritems>
                <tlbar:toolbaritem margin="5" toolbaritemimage="/ico_arrow.png"  content="arrow" toolbaritemkey="arrow"></tlbar:toolbaritem>
                <tlbar:toolbaritem margin="5" toolbaritemimage="/ico_circle.png"   toolbaritemkey="circle"></tlbar:toolbaritem>
            </tlbar:toolbarpanel.toolbaritems>
        </tlbar:toolbarpanel>
        
        <button x:name="cancelbutton"  content="cancel" click="cancelbutton_click" width="75" height="23" horizontalalignment="right" margin="0,12,0,0" grid.row="1" />
        <button x:name="okbutton"  content="ok" click="okbutton_click" width="75" height="23" horizontalalignment="right" margin="0,12,79,0" grid.row="1" />
    </grid>
</controls:childwindow>

code behind

using system;
using system.collections.generic;
using system.linq;
using system.net;
using system.windows;
using system.windows.controls;
using system.windows.documents;
using system.windows.input;
using system.windows.media;
using system.windows.media.animation;
using system.windows.shapes;

namespace popups
{
    public partial class testtoolbar : childwindow
    {
        public testtoolbar()
        {
            initializecomponent();
        }

        private void okbutton_click(object sender, routedeventargs e)
        {
            this.dialogresult = true;
        }

        private void cancelbutton_click(object sender, routedeventargs e)
        {
            this.dialogresult = false;
        }


        private void toolbarpanel_toolbaritemclick(object sender, string key)
        {
            switch (key)
            {
                case "circle":
                    break;
                case "arrow":
                    break;
            }
        }
    }
}

if we attach toolbaritemclick event to toolbar panel, it will generate an even handler as shown above in italics. the event will provide you a key and the sender. the key provides the value we set for toolbaritemkey of the toolbaritem control. based on the key we can take different actions. as toolbaritem is inherited from button you can use the default click event provided by button control.

Property (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Determine if Microservices Architecture Is Right for Your Business?
  • JUnit 5 Tutorial: Nice and Easy [Video]
  • Transactions vs. Analytics in Apache Kafka
  • Making Machine Learning More Accessible for Application Developers

Comments

Mobile Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo