Link to home
Start Free TrialLog in
Avatar of ChaimARinger
ChaimARinger

asked on

bind column in silverlight datagrid to property that accepts parameter

class cls1
property p1

class cls2
property cls1


list of cls2
the datagrid is bound to list
I want to bind col to cls1.p1
Avatar of ChaimARinger
ChaimARinger

ASKER

Let me explain the question better.
How is it possilbe to bind a column to complex data
like text="{binding prop1.prop2}" instead of a plain text="{binding prop1}"

Another posibility would be for me if prop1 has a parameter so to write something like
 text="{binding prop1[param]}"

All those solution do not raise errors but nothing is returned. I would really appreciate some help on this one.
Thanks
Avatar of MikeToole
The attached code targets WPF but should be the same, I hope, for Silverlight.

It produces 3 side-by-side listboxes
The first is populated via a Class3 property returning an instance of Class2
The second gets it's data direct from Class2
The third uses a parameter set in the XAML to determine the number of entries in the list
<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:l="clr-namespace:BindingTest"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ObjectDataProvider x:Key="DataProvider" ObjectType="{x:Type l:Class1}">
            <ObjectDataProvider.ConstructorParameters>
                <sys:Int32>7</sys:Int32>
            </ObjectDataProvider.ConstructorParameters>
        </ObjectDataProvider>
        <l:Class2 x:Key="DP2"/>
        <l:Class3 x:Key="DP3"/>
    </Window.Resources>
    <StackPanel Orientation="Horizontal" DataContext="{StaticResource DP3}">
        <ListBox ItemsSource="{Binding C2List}" DataContext="{Binding Class2Ref}"/>
        <ListBox ItemsSource="{Binding C2List}" DataContext="{StaticResource DP2}"/>
        <ListBox ItemsSource="{Binding C1List}" DataContext="{StaticResource DataProvider}"/>
    </StackPanel>
</Window>

Public Class Class1
    Dim _list As New List(Of Integer)
    Public Sub New(ByVal count As Integer)
        For i = 1 To count
            _list.Add(i)
        Next

    End Sub
    Public ReadOnly Property C1List() As List(Of Integer)
        Get
            Return _list
        End Get
    End Property
End Class

Public Class Class2
    Public ReadOnly Property C2List() As List(Of String)
        Get
            Return New List(Of String)(New String() {"AAAA", "BBBB", "CCCC", "CCCC"})
        End Get
    End Property
End Class

Public Class Class3
    Public ReadOnly Property Class2Ref() As Class2
        Get
            Return New Class2
        End Get
    End Property
End Class

Open in new window

It does not look that it works in silverlight
Which piece doesn't work?
What are the symptoms?
Any error messages?

windows does not have a recource child
ASKER CERTIFIED SOLUTION
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try

    <UserControl.Resources>
        <ObjectDataProvider x:Key="DataProvider" ObjectType="{x:Type l:Class1}">
            <ObjectDataProvider.ConstructorParameters>
                <sys:Int32>7</sys:Int32>
            </ObjectDataProvider.ConstructorParameters>
        </ObjectDataProvider>
        <l:Class2 x:Key="DP2"/>
        <l:Class3 x:Key="DP3"/>
    </UserControl.Resources>

HTH
Ashok