Wednesday, July 2, 2008

About pack uri style

As a WPF developer you might have seen the resource reference in the form

pack://application:,,,/somefile.xaml

and you might have used this style too.Actually what is this pack uri ?


This is a new method introduced in WPF to locate files in an assembly or in the current folder.Those files may be images,videos,audio and even another xaml file.

Parts

The first part identifies whether it points to an application or to the current folder where the file is located and the second part is the relative path to that file.

First part can be any one of the following

  • application -> Tells that the file is located in the assembly as Content or Resource
  • pack://application:,,,/ResourceFile.xaml
  • siteoforigin -> Its in the current executing folder
  • pack://siteoforigin:,,,/SiteOfOriginFile.xaml

      Second part tells the assembly where the file is located.If the file locate in same assembly there is no need to mention it other wise use assembly name

      Same assembly - pack://application:,,,/ResourceFile.xaml
      Different assembly - pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

      Third part is the version and this is optional

      pack://application:,,,/ReferencedAssembly;v1.0.0.1;component/ResourceFile.xaml

      Fourth part is the path to file.

      pack://application:,,,/Subfolder/ResourceFile.xaml

      Advantage of pack uri style

      • You may separate your styles from application and keep as dll.
      • Design time support
      What msdn says

      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