private void SetTemplateBinding (TemplateBindingExpression tb)
{
DependencyObject dob = target as DependencyObject;
FrameworkElement fwe = target as FrameworkElement;
if (dob == null)
throw Parser.ParseException ("Invalid TemplateBinding, expressions must be bound to DependendyObjects.");
// Applying a {TemplateBinding} to a DO which is not a FrameworkElement should silently discard the binding.
if (fwe == null)
return;
if (Parser.Context == null || Parser.Context.Template == null)
throw Parser.ParseException ("Invalid TemplateBinding, expressions can not be used outside of FrameworkTemplate.");
FrameworkElement source = Parser.Context.TemplateBindingSource;
if (source == null)
throw Parser.ParseException ("Invalid TemplateBinding, expression can not be used outside of a FrameworkTemplate.");
DependencyProperty source_prop = DependencyProperty.Lookup (source.GetKind(), tb.SourcePropertyName);
if (source_prop == null)
throw Parser.ParseException ("Invalid TemplateBinding, property {0} could not be found.", tb.SourcePropertyName);
DependencyProperty prop = LookupDependencyProperty ();
if (prop == null)
throw Parser.ParseException ("Invalid TemplateBinding, property {0} could not be found.", Name);
tb.TargetProperty = prop;
tb.SourceProperty = source_prop;
fwe.SetTemplateBinding (prop, tb);
}