public class ExtendedUserControl :UserControl
{
public ExtendedUserControl()
{
this.Loaded += new RoutedEventHandler(ExtendedUserControl_Loaded);
}
///
/// Event Delegate
///
///
///
public delegate void DataContextChangeHandler(object sender, DataContextChangedArgs e);
public event DataContextChangeHandler DataContextChanged;
///
/// Raising Event
///
///
void RaiseDataContextChanged(DataContextChangedArgs e)
{
if (DataContextChanged != null)
{
DataContextChanged(this, e);
}
}
///
/// Event Argument - Usually Object to support any type of data
///
public class DataContextChangedArgs
{
public object NewDataContext
{ get; set; }
}
///
/// Dependency Property
///
public object DTContextProperty
{
get { return (object)GetValue(DTContextPropertyProperty); }
set { SetValue(DTContextPropertyProperty, value); }
}
public static readonly DependencyProperty DTContextPropertyProperty =
DependencyProperty.Register("DTContextProperty",typeof(object),typeof(ExtendedUserControl),new PropertyMetadata(new PropertyChangedCallback(OnDTContextPropertyChanged)));
///
/// Dependency Property Call Back
///
///
///
static void OnDTContextPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
{
ExtendedUserControl source = (ExtendedUserControl)sender;
object newValue = (object)args.NewValue;
DataContextChangedArgs arg = new DataContextChangedArgs();
arg.NewDataContext = newValue;
source.RaiseDataContextChanged(arg);
}
///
/// Setting Bindings to on loaded event of the user control
///
///
///
void ExtendedUserControl_Loaded(object sender, RoutedEventArgs e)
{
var binding = new System.Windows.Data.Binding();
this.SetBinding(DTContextPropertyProperty, binding);
}
}