Sharker Khaleed Mahmud Silverlight Tips & Tricks

January 25, 2010

Tricks and Tips 16 : Enum Dependency property in custom control

In this tip we are going to create a dependency property named as TextBoxMode which will alter the height of the custom textbox control. 

CustomTextBoxControl.cs

using System.Windows;
using System.Windows.Controls;

namespace Tips
{
    public class CustomTextBoxControl : TextBox
    {
        public enum Mode { Small, Large }

        public static readonly DependencyProperty ModeProperty
            = DependencyProperty.Register(“TextBoxMode”,
            typeof(Mode),
            typeof(CustomTextBoxControl),
            new PropertyMetadata( new PropertyChangedCallback( CustomTextBoxControl.OnTextModeChanged )));

        private static void OnTextModeChanged( DependencyObject control, DependencyPropertyChangedEventArgs e )
        {
            switch ((Mode)e.NewValue)
            {
                case Mode.Small:
                    {
                        ((CustomTextBoxControl)control).Height = 30;
                        break;
                    }
                case Mode.Large:
                    {
                        ((CustomTextBoxControl)control).Height = 100;
                        break;
                    }
            }
        }

        public Mode TextBoxMode
        {
            get { return (Mode)GetValue(ModeProperty); }
            set { SetValue(ModeProperty, value); }
        }
    }
}

MainPage.xaml

<UserControl xmlns:my=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data”  x:Class=”Tips.MainPage”
    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”
    xmlns:local=”clr-namespace:Tips”
    d:DesignHeight=”300″ d:DesignWidth=”400″>
    <Grid x:Name=”LayoutRoot”>
        <ListBox x:Name=”lb” >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <local:CustomTextBoxControl TextBoxMode=”Large” Width=”100″ />  
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

MainPage.xaml.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;

namespace Tips
{
    public class ListItem
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
       
    }
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            List<ListItem> items = new List<ListItem>();
            items.Add(new ListItem { Id = “1″});
            items.Add(new ListItem { Id = “2″});
            items.Add(new ListItem { Id = “3″});
            lb.ItemsSource = items;
        }
    }
}

Sharker Khaleed Mahmud
Software Developer

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.