locked
Can we use a multi value converter in Lightswitch? RRS feed

  • Question

  • Hello,

    I've just been trying to do some data binding to a custom control, and wanted to use a multi value converter, as opposed to a single value one. In WPF, I do this with code like this...

     

      <TextBox>
        <TextBox.Text>
          <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
            <Binding Path="Property1" />
            <Binding Path="Property2" />
          </MultiBinding>
        </TextBox.Text>
      </TextBox>
    
    


    However, I just tried this in a Lightswitch custom control, and I got a compiler error telling me that the type MultiBinding wasn't found. Can't find a way to add a reference to the assembly that's supposed to hold it.

    So, can this be done? Thanks

     


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Sunday, October 9, 2011 5:49 PM

Answers

  • Silverlight doesn't have multi-value converters, like WPF has, but Colin Eberhardt wrote some code to implement the functionality in Silverlight.

    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    • Marked as answer by Mr Yossu Monday, October 10, 2011 1:13 PM
    Monday, October 10, 2011 12:41 PM
    Moderator

All replies

  • Silverlight doesn't have multi-value converters, like WPF has, but Colin Eberhardt wrote some code to implement the functionality in Silverlight.

    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    • Marked as answer by Mr Yossu Monday, October 10, 2011 1:13 PM
    Monday, October 10, 2011 12:41 PM
    Moderator
  • Thanks for that Yann, it looks like just what I need!

    On a related note, do you know how to get at custom control properties in a value converter? Say you had a custom control based on a textbox, and you had set up properties for the screen designer in the lsml file. You can bind the Text property of the textbox to the value being passed from the screen, and you can pass that through a value converter, but what if you wanted to get hold of the value of one of the screen designer properties in the converter? How would you do that?

    Thanks again.


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Monday, October 10, 2011 1:13 PM
  • Forgot to add, for anyone interested, the code Yann was referring to can be found here.
    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Monday, October 10, 2011 1:14 PM
  • You have to bind to the control itself, you can't do two-way binding, then in the converter:

    VB:

    Const CONTROL_KEY As String = "AssemblyName:ControlName/PropertyName"
    
    Dim control = TryCast(value, IContentItem)
    If (control Is Nothing) Then Return Nothing
    
    Dim propertyValue = control.Properties(CONTROL_KEY)
    
    

    C#:

    const string CONTROL_KEY = "AssemblyName:ControlName/PropertyName";
    
    var control = value as IContentItem;
    if (control == null)
    {
        return null;
    }
    
    var propertyValue = control.Properties(CONTROL_KEY);
    


    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Monday, October 10, 2011 1:35 PM
    Moderator
  • Thanks for the quick reply.

    Not sure what you mean by binding to the control itself, but let me give you a more specific case, and see if you could show me how you would do this. Suppose you had a custom control that included a label, and you wanted to give the designer the opportunity to specify the maximum number of characters to be shown in the label, if the actual string value was too long.

    You would add an integer screen designer property called (say) MaxLength, and you might also have a bool property called Truncate, so the user could specify whether or not they want the text truncating. Then in your converter, you would need to be able to get hold of the data value, as well as the value of the two properties. How would you do that?

    Sorry if I'm being dumb, but I didn't quite catch what you meant. Thanks again


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Monday, October 10, 2011 1:43 PM