[英]How to put items from an observable collection in a grid?
While I was writing an application for Windows Phone 8, I encountered a problem. 在为Windows Phone 8编写应用程序时,遇到了一个问题。 In ViewModel I have 在ViewModel中,我有
public ObservableCollection<Ball> ListOfBalls { get; set;}
Every Ball has their row number and column number. 每个球都有其行号和列号。
In View I have specified Grid.Row
into which I want to put a grid (10 rows x 10 columns) and then I want to put one ball from ListOfBalls in every grid cell. 在View中,我指定了Grid.Row
,我要在其中放置一个网格(10行x 10列),然后在每个网格单元中放置一个来自ListOfBalls的球。
So, that is View
: 因此,这就是View
:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="5*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="GamePlayGrid"
Grid.Row="1"
Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- I've tried to add ItemsControl here -->
</Grid>
</Grid>
I try to define Grid (10 rows x 10 columns) and then in scope of this grid I tried to add ItemsControl
with DataTemplate
, but I'm not sure if it's right, because during execution I got some unhandled exception... How can I fix this? 我尝试定义Grid(10行x 10列),然后在此网格范围内尝试使用DataTemplate
添加ItemsControl
,但是我不确定是否正确,因为在执行过程中我遇到了一些未处理的异常...我解决这个吗?
Edited, from your code, Added Image 根据您的代码进行编辑 ,添加了图片
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ItemsControl ItemsSource="{Binding ListOfBalls}"/>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Height="30" Width="30" Source="{Binding Path=BallImageName}"/>
<TextBlock Text="{Binding BallName}"/>
<StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
BallImageName is a property of ball image, Please add the source of the located file. BallImageName是球图像的属性,请添加所定位文件的源。 (eg:"Images/myimage.png") (例如:“ Images / myimage.png”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.