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);
Margin property can't be changed by this way, you could do this:
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>
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);
Margin property can't be changed by this way, you could do this:
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>
0 commentaires:
Enregistrer un commentaire