Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas binding in silverlight

I'm trying to create a canvas, with items located at specefied locations on the canvast, as i can not bind an source and a template directly to a Canvas, have i used a ItemsControl. But there are a problem all the items are located at 0,0. And i have tested the Bindings they do not return 0,0. How can i make this work so the items are located at the right place?

Also is it poissible to create 2 layers on the canvas, where each layer is binded to a diffrent source, and uses a diffrent template?

This is in Silverlight

<ItemsControl Grid.Row="1" Grid.Column="1"
                Width="650" Height="650"
                ItemsSource="{Binding Skills}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas Margin="0"
                Width="650" Height="650" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Canvas.Top="{Binding Top}" Canvas.Left="{Binding Left}">
                    <TextBlock Text="{Binding Name}" />
                <Image Source="{Binding Icon}" />
                <StackPanel Orientation="Horizontal" >
                    <TextBlock FontWeight="Bold" TextAlignment="Center" Text="{Binding SkillPointsStatusText}" />
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Test with ItemContainerStyle

<ItemsControl Grid.Row="1" Grid.Column="1"
                Width="650" Height="650"
                ItemsSource="{Binding Skills}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas Margin="0"
                Width="650" Height="650" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}" />
                <Image Source="{Binding Icon}" />
                <TextBlock FontWeight="Bold" TextAlignment="Center" Text="{Binding SkillPointsStatusText}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Canvas.Top" Value="{Binding Top}" />
            <Setter Property="Canvas.Left" Value="{Binding Left}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

Well i have droped the project, but i will leave the question open should one have an anwser

like image 1000
Androme Avatar asked Jan 22 '11 22:01

Androme


1 Answers

All of the following does not work in SL4 since it depends on bindings in a Setter.Value.


Try setting the binding in the ItemContainerStyle since your StackPanel is not the root element; your template will be placed in a ContentPresenter, so your attached properties for canvas positioning in the StackPanel will be ignored.

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="Canvas.Top" Value="{Binding Top}" />
        <Setter Property="Canvas.Left" Value="{Binding Left}" />
    </Style>
</ItemsControl.ItemContainerStyle>

Edit: If Silverlight does not support ItemContainerStyle you can set the universal style for ContentPresenters which should work just as well:

    <ItemsControl ItemsSource="{Binding Data}">
        <ItemsControl.Resources>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" Value="{Binding Left}"/>
                <Setter Property="Canvas.Top" Value="{Binding Top}"/>
            </Style>
        </ItemsControl.Resources>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                ...
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
like image 60
H.B. Avatar answered Oct 01 '22 07:10

H.B.