views:

92

answers:

1

Hi,

I'm trying to create a GUI (WPF) Library where each (custom) control basically wraps an internal (third party) control. Then, I'm manually exposing each property (not all of them, but almost). In XAML the resulting control is pretty straightforward:

<my:CustomButton Content="ClickMe" />

And the code behind is quite simple as well:

public class CustomButton : Control
{    
    private MyThirdPartyButton _button = null;

    static CustomButton()
    {        
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), new FrameworkPropertyMetadata(typeof(CustomButton)));
    }

    public CustomButton()
    {
        _button = new MyThirdPartyButton();
        this.AddVisualChild(_button);        
    }

    protected override int VisualChildrenCount
    {    
        get
            { return _button == null ? 0 : 1; }
    }

    protected override Visual GetVisualChild(int index)
    {
        if (_button == null)
        {
            throw new ArgumentOutOfRangeException();
        }
        return _button;
    }

    #region Property: Content
    public Object Content
    {
        get { return GetValue(ContentProperty); }
        set { SetValue(ContentProperty, value); }
    }

    public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(
                        "Content", typeof(Object),
                    typeof(CustomButton), 
                        new FrameworkPropertyMetadata(new PropertyChangedCallback(ChangeContent))
    );

    private static void ChangeContent(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        (source as CustomButton).UpdateContent(e.NewValue);
    }

    private void UpdateContent(Object sel)
    {
        _button.Content = sel;
    }
    #endregion
}

The problem comes after we expose MyThirdPartyButton as a property (in case we don't expose something, we would like to give the programmer the means to use it directly). By simply creating the property, like this:

public MyThirdPartyButton InternalControl
{
    get { return _button; }
    set
    {
        if (_button != value)
        {
            this.RemoveVisualChild(_button);
            _button = value;
            this.AddVisualChild(_button);
        }
    }
}

The resulting XAML would be this:

<my:CustomButton>
<my:CustomButton.InternalControl>
    <thirdparty:MyThirdPartyButton Content="ClickMe" />
</my:CustomButton.InternalControl>

And what I'm looking for, is something like this:

<my:CustomButton>
<my:CustomButton.InternalControl Content="ClickMe" />

But (with the code I have) its impossible to add attributes to InternalControl...

Any ideas/suggestions?

Thanks a lot,

-- Robert

+2  A: 

WPF's animation system has the ability to set sub-properties of objects, but the XAML parser does not.

Two workarounds:

  1. In the InternalControl property setter, take the value passed in and iterate through its DependencyProperties copying them to your actual InternalControl.
  2. Use a build event to programmatically create attached properties for all internal control properties.

I'll explain each of these in turn.

Setting properties using the property setter

This solution will not result in the simplified syntax you desire, but it is simple to implement and will probably solve the main problem with is, how to merge values set on your container control with values set on the internal control.

For this solution you continue to use the XAML you didn't like:

<my:CustomButton Something="Abc">
  <my:CustomButton.InternalControl> 
    <thirdparty:MyThirdPartyButton Content="ClickMe" /> 
  </my:CustomButton.InternalControl> 

but you don't actually end up replacing your InternalControl.

To do this, your InternalControl's setter would be:

public InternalControl InternalControl
{
  get { return _internalControl; }
  set
  {
    var enumerator = value.GetLocalValueEnumerator();
    while(enumerator.MoveNext())
    {
      var entry = enumerator.Current as LocalValueEntry;
      _internalControl.SetValue(entry.Property, entry.Value);
    }
  }
}

You may need some additional logic to exclude DPs not publically visible or that are set by default. This can actually be handled easily by creating a dummy object in the static constructor and making a list of DPs that have local values by default.

Using a build event to create attached properties

This solution allows you to write very pretty XAML:

<my:CustomButton Something="Abc"
                 my:ThirdPartyButtonProperty.Content="ClickMe" />

The implementation is to automatically create the ThirdPartyButtonProperty class in a build event. The build event will use CodeDOM to construct attached properties for each property declared in ThirdPartyButton that isn't already mirrored in CustomButton. In each case, the PropertyChangedCallback for the attached property will copy the value into the corresponding property of InternalControl:

 public class ThirdPartyButtonProperty
 {
   public static object GetContent(...
   public static void SetContent(...
   public static readonly DependencyProperty ContentProperty = DependencyProperty.RegisterAttached("Content", typeof(object), typeof(ThirdPartyButtonProperty), new PropertyMetadata
   {
     PropertyChangedCallback = (obj, e) =>
     {
       ((CustomButton)obj).InternalControl.Content = (object)e.NewValue;
     }
   });
 }

This part of the implementation is straightforward: The tricky part is creating the MSBuild task, referencing it from your .csproj, and sequencing it so that it runs after the precompile of my:CustomButton so it can see what additional properties it needs to add.

Ray Burns
Thanks Ray. The second Workaround works like charm. And yeah, it will be tricky to generate all those Properties...The first Workaround doesn't work directly in the setter. It might be because at that point the object hasn't been initialized yet. Anyway, if you iterate through GetLocalValueEnumerator() after its initialized (value.Initialized += HANDLER) it works. Please correct me if I'm wrong.I still have to check if the bindings on "InternalControl" properties work correctly. Actually, I have to check:-Bindings-Styles-Targeted Styles-ControlTemplates-ItemTemplates-Validations-???
Robert