Archive

Posts Tagged ‘XAML’

Subclassing WPF user control

September 21, 2011 3 comments

It is not very common to do User Control subclassing but if you have some compelling reason to do so then following detail explains how to do it with WPF User Control. Due to xaml, user control subclassing in WPF becomes little tricky. Following steps explain the procedure :

1. Create a base control as simple c# class and not WPF user control. Derive this base class from UserControl.

    public class ESQBaseControl : UserControl
    {
		#region Constructor
		
        protected ESQBaseControl()
        {
            // protected constructor is required so that derived controls can have default parameterless constructor which is needed for xaml 
            // also protected constructor will ensure that new control creation always use constructor by passing parameters (e.g. in this example passing of string parameter is must in order to construct control)
        }

        public ESQBaseControl(string info)
        {
            
        }
		
		#endregion
		
		#region base implementation
		
		public virtual void SomeOperation()
		{
		
		}

		#endregion
	}

2. Base class will not have UI implementation because of the absence of xaml. So time to build some UI for our control in concrete control xaml.

	
	<view:ESQBaseControl x:Class="<namespace>.ESQConcreteControl"
             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" 
             xmlns:view="clr-namespace:<namespace in which base control resides>"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
		<Grid>
			
			<Grid.RowDefinitions>
				<RowDefinition/>
				<RowDefinition/>
			</Grid.RowDefinitions>

			<Label Content="My Control"/>
			<TextBox x:Name="MyTextBox" Grid.Row="1" Text="Some Text"/>
		</Grid>
	</view:ESQBaseControl>

In above code note that whole xaml has been wrapped inside BaseControl class while class name inside xaml definition is given of the concrete class.

3. Finally, inside the code behind file user control’s concrete logic can be implemented :

	public partial class ESQConcreteControl : ESQBaseControl
    {
        #region Constructor

        public ESQConcreteControl(string info) : base(info)
        {
            InitializeComponent();
        }
        
        #endregion
		
		#region Control implementation
		
		public override void SomeOperation()
		{
			myTextBox.Text = "Hello World!!";
		}
		
		#endregion
	
	}
	

Hope this helps!!

Advertisements

XAML Compilation

December 22, 2008 Leave a comment

XAML compilation involves three things :

  1. Converting XAML file in special binary format (namely BAML)
  2. embedding the converted content as a binary resource in the assembly being built
  3. performing the plumbing work that connects XAML with procedural code

Point to ponder :-

Normally XAML file declartaion starts with x:Class but it can also do without it. When x:Class is not used it means it’s pure xaml file without any code behind file. It means features related to procedural code can not be used.

Categories: WPF, Xaml Tags: , ,