locked
Bind a custom control to a query RRS feed

  • Question

  • Hello,

    how do I bind a custom control to a whole query or collection? In the example from http://blogs.msdn.com/b/lightswitch/archive/2011/01/13/using-custom-controls-to-enhance-lightswitch-application-ui-part-1.aspx the custom control gets bound to a single value.

    In my scenario I want to bind appointments to the Silverlight Calendar component from DevComponents.
    For some reason I can't add the component directly to the LightSwitch project (Could not resolve member reference: mscorlib, ...ComVisible), so I've created a Wrapper project that contains a Silverlight User Control with the Calendar.

    My steps are the following:

    • Drag the query to the design surface of LightSwitch
    • Change the control type to "Custom Control"
    • Change the Custom Control type to my wrapper type
    • Set the binding in code in the Created partial method of the screen:

     

    var scheduler = this.FindControl("Scheduler");
    scheduler.SetBinding(CalendarView.CalendarModelProperty, "CalendarModel", new AppointmentsConverter(), BindingMode.TwoWay);

    Implement a IValueConverter that converts the VisualCollection<T> to the CalendarModel type.

    But nothing happens. Is this the right approach or do I have to keep in mind when working with custom controls to colle

    Monday, April 4, 2011 12:38 PM

Answers

  • The binding string is incorrect in your code.

    Basically, the DataContext of the control in LS runtime is IContentItem.  That is the root of the binding string.  It does not have CalendarModel property.  It does have a value property to contain the detail value.  It is also not possible to be a TwoWay binding, because you cannot create VisualCollection to replace the original one.  Of course, you may add/remove items inside the collection.  So it should be a OneWay binding.

    scheduler.SetBinding(CalendarView.CalendarModelProperty, "Value", new AppointmentsConverter(), BindingMode.OneWay);


    LLF
    Tuesday, April 5, 2011 6:29 PM
  • I've got it to work by providing the right binding path ("Screen.<VisualCollection>"). I had to make a custom empty stub class and inherit from the CalendarView control and a Value converter, so I was able to add the control to the LS designer surface and bind to the exposed properties of the CalendarView.
    • Marked as answer by Tobias Burger Tuesday, April 19, 2011 2:08 PM
    Tuesday, April 19, 2011 2:08 PM

All replies

  • The binding string is incorrect in your code.

    Basically, the DataContext of the control in LS runtime is IContentItem.  That is the root of the binding string.  It does not have CalendarModel property.  It does have a value property to contain the detail value.  It is also not possible to be a TwoWay binding, because you cannot create VisualCollection to replace the original one.  Of course, you may add/remove items inside the collection.  So it should be a OneWay binding.

    scheduler.SetBinding(CalendarView.CalendarModelProperty, "Value", new AppointmentsConverter(), BindingMode.OneWay);


    LLF
    Tuesday, April 5, 2011 6:29 PM
  • I've got it to work by providing the right binding path ("Screen.<VisualCollection>"). I had to make a custom empty stub class and inherit from the CalendarView control and a Value converter, so I was able to add the control to the LS designer surface and bind to the exposed properties of the CalendarView.
    • Marked as answer by Tobias Burger Tuesday, April 19, 2011 2:08 PM
    Tuesday, April 19, 2011 2:08 PM