Canvas binding in silver light

I am trying to create a canvas with elements located in special places on canvast, since I cannot link the source and template directly to Canvas, I used ItemControl. But there is a problem that all items are at 0.0. And I tested the bindings that they do not return 0,0. How can I do this work so that the elements are in the right place?

It is also permissible to create 2 layers on the canvas, where each layer is tied to a different source and uses a different 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 chose a project, but I will leave the question open if you have anwser

+8
data-binding wpf silverlight xaml
source share
1 answer

In SL4, everything does not work, since it depends on the bindings in Setter.Value .


Try to set the binding in ItemContainerStyle , since your StackPanel not the root element; your template will be placed in ContentPresenter , so your attached properties for positioning the canvas 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 a 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> 
+7
source share

Source: https://habr.com/ru/post/651386/


All Articles