Thursday, December 20, 2007

DataBinding in UserControl

Here are some guidelines to expose properties of inner Controls(Controls which are inside UserControl) through WPF UserControl.

Note: UserControl is not same as Control in which we are using Templates..
UserControl just holds other controls and wrap them.
  1. Create DP in UserControl.xaml.cs file same as of its child control.
  2. Bind this new DP with the inner control's DP
  3. Now you could use DP of inner control out side of UserControl

Here attached a sample which demonstrates this.

MyUserControl holds a slider and we are going to expose the Value DP of this through MyUserControl.

So add Value DP to MyUserControl and Bind that to Value DP of inner slider.


public partial class MySlider : System.Windows.Controls.UserControl
{
public MySlider()
{
InitializeComponent();
}
public static readonly DependencyProperty ValueProperty=DependencyProperty.Register("Value",typeof(double),typeof(MySlider));
public double Value
{
get
{
return (double)GetValue(MySlider.ValueProperty);
}
set
{
SetValue(MySlider.ValueProperty, value);
}
}
}



MyUserControl.XAML

<UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="BindingUserControl.MySlider"
Width="Auto" Height="Auto" x:Name="UserControl" >

<Grid>
<Slider Name="sldr" Value="{Binding Path=Value, ElementName=UserControl, Mode=TwoWay}" Width="100" />
</Grid>
</UserControl>

Sample

Tuesday, December 11, 2007

Using Angle selector control of Blend in your application

If you have ever worked in Blend ,you would be familiar with the Angle selection control in blend.Sometimes this is refered as AngleSlider or in some other name.
But I think MSFT calls this as "RotationSpinner".(I got this name from a blend specific dll)

See this if you are still not able to identify the control.


This is a nice control which takes less space than slider and gives a great Visual appeal.Atleast once in a project time,you might thought of adding this control into your project :-)

Here an easy method to add this control into your project.I dont know whether MSFT allows this or not :-) .If Anybody have idea pls respond in comments.



  1. In your project add reference to "Microsoft.Expression.DesignSurface.dll".(Locate at C:\Program Files\Microsoft Expression\Blend 2 September Preview if you are using Sep preview)

  2. Select Asset library from toolbox and click on "CustomControls"

  3. Select "RotationSpinner" control and create its instance.

  4. Now you got the required control.Set its properties and go on..