vikram.ma
vikram.ma

Reputation: 81

Setting template binding in code behind

The output 'm expecting is something like this,

<Canvas Width="800" Height="600">
   <Ellipse Stroke="#FF000000" StrokeThickness="2" Width="284" Height="288" 
            ToolTip="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Min}" 
            Canvas.Left="312" Canvas.Top="122" />
</Canvas>

With this code,

//This will ultimately hold object of type UIElement, which is Ellipse in this case.
private DependencyObject selectedObject; 

public void AddBinding(DependencyProperty dependencyProperty, DependencyProperty ipartProperty)
{
    Binding binding = new Binding(ipartProperty.Name); //Here Name is Min, an attached property
    binding.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
    BindingOperations.SetBinding(selectedObject, dependencyProperty, binding);
}

But actual output is

<Canvas Width="800" Height="600">
   <Ellipse Stroke="#FF000000" StrokeThickness="2" Width="284" Height="288" 
            ToolTip="{x:Null}" Canvas.Left="312" Canvas.Top="122"/>
</Canvas>

I don't know whats wrong, can someone please help

Upvotes: 0

Views: 2912

Answers (2)

vikram.ma
vikram.ma

Reputation: 81

Found the answer. Use the following class

public class BindingConverter : ExpressionConverter
{
    public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, Type destinationType)
    {
        return true;
    }

    public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(MarkupExtension))
        {
            BindingExpression bindingExpression = value as BindingExpression;
            if (bindingExpression == null)
            {
                throw new FormatException("Expected binding, but didn't get one");
            }
            return bindingExpression.ParentBinding;
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

Add this method to the class from you call XamlWriter.Save(obj)

private void Register()
    {
        Attribute[] attr = new Attribute[1];
        TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(BindingConverter));
        attr[0] = vConv;
        TypeDescriptor.AddAttributes(typeof(BindingExpression), attr);
    }

And got the answer i wanted!!! The credit goes to Alex Dov http://www.codeproject.com/script/Membership/View.aspx?mid=106815 . Thanks a lot to this guy

Upvotes: 1

brunnerh
brunnerh

Reputation: 184286

Output of what?

If you are using some kind of XamlWriter you should watch its limitations, they do not preserve bindings, if you mean that this is the value of the property that would make sense too as you cannot bind to attached properties like that, you need this path: (OwndingClass.AttachedProperty) (note parentheses and owning class prefix)

Upvotes: 0

Related Questions