views:

93

answers:

3

In silverlight, can you bind to a property that contains parameter? For example, the following doesn’t seem to work. Am I missing something or is this not possible?

C#

private System.Collections.Generic.Dictionary<string, string> ValuesField = new System.Collections.Generic.Dictionary<string, string>();
public string Value {
    get { return ValuesField(FieldName); }
    set { ValuesField(FieldName) = value; }
}

VB

Private ValuesField As New System.Collections.Generic.Dictionary(Of String, String)
Public Property Value(ByVal FieldName As String) As String
        Get
            Return ValuesField(FieldName)
        End Get
        Set(ByVal value As String)
            ValuesField(FieldName) = value
        End Set
End Property

XAML

<TextBox Name="TextBox1" VerticalAlignment="Top" Width="120"Text="{Binding Path=Value[MyField],Mode=TwoWay }"  />
A: 

Your C# is a little wonky - you need square brackets: ValuesField[FieldName]

There is no reason that you cannot oneway bind to a value returned from an indexed collection, but with SL3 you are very limited with the value you can pass in to specify the index to use.

Check this MSDN article for the SL3 capabilities, and this link for a bit of an example of what is possible in SL4. In SL4 you get the ability to use string indexes, but it doesn't look like you can make them dynamic, they have to be statically (hard) coded. Note that SL has a subset of the binding capability that WPF has, so what you see in WPF cannot necessarilty be applied in SL.

slugster
A: 

VB has the syntax needed to create a parameterised property, but C# does not support parameterised properties.

XAML parse also does not recognise parameterised properties.

So the simple answer to your quesiton is No.

One way to emulate this in C# is to expose Type on the (now parameterless) property that has an Indexer that takes the parameters required. Note for compatability with XAML the indexer(s) are limited to having a single parameter of type int and/or string.

With that you can use a property path like "Property[parametervalue]" in a binding.

AnthonyWJones
+1  A: 

Try to use IValueConverter for binding

MagicMax