简体   繁体   中英

Defining coordinates of a Silverlight image

I want to draw a picture in code-behind in Silverlight. Because I will draw so many from them based on a web service information. But for this, when I used image.Margin.Left and top its giving error. How can I handle it?

        Image image = new Image();
        image.Source = new BitmapImage(new Uri("wall.png", UriKind.Relative));
        image.Height = 120;
        image.Width = 120;
        image.Margin.Left = 20;

        LayoutRoot.Children.Add(image);

保证金属性无法通过这种方式更改,您可以这样做:

image.Margin = new Thickness(left, top, right, bottom);

The other answer about setting the margin in code is 100% correct. So this is rather meant as a stimulus to explore the possibilities the framework offers for such a "picture gallery".

Arranging pictures programmatically - You could use a Canvas :

var image = new Image {
    Source = new BitmapImage(new Uri("wall.png", UriKind.Relative)),
    Height = 120,
    Width = 120
};
Canvas.SetLeft(image, 20);
Canvas.Children.Add(image);

As soon as you want pictures to overlap you can easily control which one is on top by setting the z index via Canvas.SetZIndex(image,42) (in a Grid you would need to rearrange the child order, which is more of a hassle).

Arranging pictures via an ItemsControl - out-of-the-box or custom:

<ItemsControl ItemsSource="{Binding Path=WebServiceResult.Images}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate><WrapPanel/></ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <Image Source="{Binding Path=SourceUri}" Height="120" Width="120" Margin="20,0,0,0"/>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM