Can you bind data to a property containing a parameter in Silverlight?

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

FROM#

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; }
}

      

V. B.

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 }"  />

      

+2


a source to share


3 answers


Try to use IValueConverter for binding



+1


a source


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

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



Check out this MSDN article for SL3 features and this link for an example of what's possible in SL4. In SL4 you get the ability to use string indexes, but it doesn't seem like you can make them dynamic, they have to be statically (hardcoded). Note that SL has a subset of the binding capability that WPF has, so what you see in WPF may not necessarily be applied in SL.

0


a source


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

XAML parsing also does not recognize parameterized properties.

Well, the simple answer to your quesiton: None .

One way to emulate this in C # is to infer the Type to the (nowlessless) property that has an Indexer that takes the required parameters. XAML compatibility note, the indexer is limited to having a single parameter of type int and / or string.

With this, you can use a property path such as "Property [parametervalue]" in the binding.

0


a source







All Articles