views:

17

answers:

1

I have a list of object and want to bind for display purposes to a list of custom controls.

XAML code:

                <Pages:MyItemsControl ItemsSource="{Binding SquadFieldPlayers}">
                    <Pages:MyItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Canvas Height="180" Width="169" />
                        </ItemsPanelTemplate>
                    </Pages:MyItemsControl.ItemsPanel>
                    <Pages:MyItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Pages:FieldItem />
                        </DataTemplate>
                    </Pages:MyItemsControl.ItemTemplate>
                </Pages:MyItemsControl >

Source code of 'MyItemsContol':

    public class MyItemsControl : ItemsControl
{
    protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
    {
        FrameworkElement contentitem = element as FrameworkElement;
        if (contentitem != null)
        {
            Binding leftBinding = new Binding("PositionX");
            Binding topBinding = new Binding("PositionY");
            contentitem.SetBinding(Canvas.LeftProperty, leftBinding);
            contentitem.SetBinding(Canvas.TopProperty, topBinding);
            base.PrepareContainerForItemOverride(element, item);             
        }
    }
}

In another topic. Here I've bound position-properties of my object to a Left and Top properties (to be displayed in proper location on the canvas).

Question1: how can I setup another property of FieldItem control (I have textBlock inside and want him to display other data items from data objects)?

Here is my FieldItem control:

<UserControl x:Class="VfmElitaSilverlightClientView.Pages.FieldItem" ...>    
<Grid Height="16" Width="16">
    <Ellipse Fill="Yellow" Height="16" Width="16">
    </Ellipse>
    <TextBlock Name="TeamNumberTextBlock" Text="22" TextAlignment="Center" FontStyle="Italic" />
</Grid>

and I want to populate TeamNuberTextBlock with meaningful data.

Question2: why DataContext object of FieldItem is not set to my data object?

Thank you!

A: 

I don't know how does that work, but to get my control bound to data object nothing actually is required. Properties mapping is done with usual binding:

<UserControl x:Class="VfmElitaSilverlightClientView.Pages.FieldItem" ...>    
<Grid Height="16" Width="16">
    <Ellipse Fill="Yellow" Height="16" Width="16">
    </Ellipse>
    <TextBlock Name="TeamNumberTextBlock" Text="**{Binding TeamNumber}**"
               TextAlignment="Center" FontStyle="Italic" />
</Grid>

I will appreciate an explanations of that. Thank you in advance for you efforts!

Budda
Explanation: Originally I've expected data object binding to control to be set-up in the 'PrepareContainerForItemOverride' method of the 'MyCustomList' class. But actually that is done later, and usual binding is applied for this case.
Budda