【发布时间】:2014-11-18 20:56:02
【问题描述】:
我的应用程序是 Windows Phone 8.1 中的通用应用程序。
我想在屏幕上画一些圆圈,但要根据我的 ViewModel 中的数据设置它们的边距,我知道为一个项目做这件事很热,但对其中的很多项目都不做。
所以这就是没有任何绑定的样子:
<Grid>
<Canvas>
<!-- Background -->
<Rectangle Width="400" Height="400" Fill="Gray"/>
<!-- List of circles -->
<Ellipse Fill="White" Width="10" Height="10" Margin="40,300,0,0"/>
<Ellipse Fill="White" Width="10" Height="10" Margin="80,250,0,0"/>
<Ellipse Fill="White" Width="10" Height="10" Margin="120,240,0,0"/>
<Ellipse Fill="White" Width="10" Height="10" Margin="160,275,0,0"/>
<Ellipse Fill="White" Width="10" Height="10" Margin="200,275,0,0"/>
<Ellipse Fill="White" Width="10" Height="10" Margin="240,130,0,0"/>
<Ellipse Fill="White" Width="10" Height="10" Margin="280,150,0,0"/>
<Ellipse Fill="White" Width="10" Height="10" Margin="320,200,0,0"/>
<Ellipse Fill="White" Width="10" Height="10" Margin="360,220,0,0"/>
</Canvas>
</Grid>
但我不想有多个这样的控件,我在我的 ViewModel 中做了这个:
public ObservableCollection<Point> Points { get; set; }
public ObservableCollection<Thickness> Thickness { get; set; }
public MainViewModel ()
{
if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
{
Points = new ObservableCollection<Point>();
Points.Add(new Point(40, 250));
Points.Add(new Point(60, 300));
Points.Add(new Point(90, 150));
Thickness = new ObservableCollection<Thickness>();
foreach(Point point in Points)
{
Thickness.Add(new Thickness(point.X, point.Y, 0, 0));
}
}
}
我想为每个圆(或椭圆)绑定属性“厚度”中设置的边距。 画布中的圆圈数必须与 ObservableCollection "Thickess" 中的项目数相同。
我不知道如何说得足够清楚,所以如果您有一些问题,请不要犹豫。
【问题讨论】:
-
如果你不确定如何说清楚,你可以尝试阅读"How to create a Minimal, Complete, and Verifiable example"
标签: c# list xaml binding windows-phone-8.1