views:

5204

answers:

5

Hi all, I'm writing my very first Silverlight app. I have a datagrid with a column that has two labels, for the labels, i am using an IValueConverter to conditionally format the data.

The label's "Content" is set as such:

Content="{Binding HomeScore, Converter={StaticResource fmtshs}}"

and

Content="{Binding AwayScore, Converter={StaticResource fmtshs}}"

The Convert method of my IValueConverter is such:

Public Function Convert(
  ByVal value As Object, 
  ByVal targetType As System.Type, 
  ByVal parameter As Object, 
  ByVal culture As System.Globalization.CultureInfo) As Object 
Implements System.Windows.Data.IValueConverter.Convert

    Dim score As Long = value, other As Long = parameter

    Return If(score < 0, "", 
        If(score - other > 5, (other + 5).ToString, score.ToString)
    )

End Function

So what i want to do is in the converter for HomeScore, i want to pass AwayScore to the ConverterParameter, and for AwayScore i want to pass the HomeScore to the converter. In the converter for either score i need to be able to know the value of the other score for formatting purposes.

But i cannot figure out the syntax for binding the ConverterParameter to another field.
I've tried the following:

Content="{Binding HomeScore, Converter={StaticResource fmtshs}, ConverterParameter=AwayScore}"  
Content="{Binding HomeScore, Converter={StaticResource fmtshs}, ConverterParameter={AwayScore}}"  
Content="{Binding HomeScore, Converter={StaticResource fmtshs}, ConverterParameter={Binding AwayScore}}"

But none of those seem to work. How do i pass a field value to the ConverterParameter?

A: 

Not totally sure I understand your problem but I think you're looking for binding with "elementname"?

ex: http://www.wintellect.com/CS/blogs/jprosise/archive/2009/03/27/silverlight-3-s-new-element-data-binding.aspx

vidalsasoon
+4  A: 

Looks like you're trying to bind to a ConverterParameter which I'm afraid you can't. ConverterParameter can only take literal values e.g ConverterParameter='Your string'

Andy Britcliffe
Yes, this was exactly what I was/am trying to do.
eidylon
if you haven't figured it out yet, the single quotes are what you're missing in your question code.
Kevin
A: 

I had the same problem, and had to sleep on it. Seems the Converter gets "one shot" at getting the data - through the Binding value.

So make the Binding value a complex class. If you are using M-V-VM you should be data-shaping, anyway, so I made the Binding value 'work harder' by including the display value and the other data I needed in my converter (created a contained class if you will).

Next, I needed to have the Converter 'work harder', with its limitation of passing ConverterParameters as Value type literals, so I create an Enum in my converter, and cast the literal onto it for more elegancy in my Convert routine.

What I could do then is vary color (Brush) and thickness of a Grid cell, based on the value being displayed and another threshold value (that I check against).

The source code is on my blog site, its Silverlight 3 code using binding in a pseudo M-V-VM fashion (no dependency injection, but hey - its an example, right?)

download at: http://www.martymazurik.com/file.axd?file=2010%2f6%2fHighlightGridCell.zip.txt

then remove the .txt

Marty Mazurik
A: 

Kevin's comment to the answer did the trick for me. It's possible to bind to a ConverterParameter, but you need to use single-quotes to surround the nested binding statement.

<Image Source="{Binding SomeProperty, Converter={StaticResource SomeConverter}, ConverterParameter='{Binding SomeOtherProperty}'}"/>

Edit: It turns out this does not work in SL3, as you cannot pass a databound value to a ConverterParameter.

Will Rogers
- 1, this is not correct. The string is '{Binding SomeOtherProperty}' is passed as parameter to the Convert-Method of the IValueConverter. And not the value of the bound property.
Jehof
You're right Jehof - I finally was able to test this and it does not work. SL3 does not support a bound ConverterParameter.
Will Rogers
-1, that doesn't work. It shouldn't be an accepted answer
Olivier PAYEN
+1  A: 

I have tried the single-quotes but in the converter it gives me a string of '{Binding SomeOtherProperty}'. Did I miss anything? :(

PS. I am using Silverlight 3.

Xin