30 November 2011

Binding, and Templates, and ContentControls, oh my!

I'm working in Silverlight 4, and have been trying to get a particular binding scenario to work right. It seems to be the case that try to satisfy any one or two requirements is easy enough, but when things start piling up, Silverlight can get a bit tricky.

My requirements:

  1. I'm creating a UserControl
  2. The UserControl itself needs to work inside a template.
  3. I want to define properties on the UserControl.
  4. I want a ContentControl in the UserControl
  5. I want to define a ContentTemplate on the content control
  6. And lastly, I want this inner template to bind to properties of the UserControl

The problems:

  • I can't name the UserControl and use ElementName inside the template, because the UserControl itself is also going to be used in a template, and ElementName doesn't always behave well in that situation.
  • I can't programatically set the DataContext or Bindings within the template, because Silverlight doesn't give programatic access to a template.
  • Setting the DataContext of the UserControl or the ContentControl to the UserControl itself doesn't help, because the DataContext of a template is actually the Content property of the ContentControl.
  • I can't set the Content of the ContentControl to the UserControl itself, because this makes Silverlight hang (presumably due to an infinite recursive loop).

So here's what I've got working. It's disgusting, but seems to work.

<UserControl>
...
<ContentControl x:Name="content"/>
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBox Text="{Binding Self.SomeValue}" />
</DataTemplate>
<ContentControl.ContentTemplate>
</ContentControl>
...
</UserControl>

And the code...

public partial class MyControl : UserControl
{
public class Indirection
{
public ValueEditor Self { get; set; }
}

public MyControl()
{
InitializeComponent();
this.content.Content = new Indirection() { Self = this };
}

public static readonly DependencyProperty SomeValueProperty = ...
public string SomeValue { ... }

}

So the idea is to set the ContentControl.Content to a proxy object that is not the control itself, but with a reference to the control, which can then be accessed in the path of the binding.

If you know a nicer way to do this, please leave a comment!

No comments: