上一回提到用平移变换把一个控件移动到容器内指定的位置,个人以为这种方法虽然巧妙,但是算不上主流。于是我去研究了一下System.Windows.Controls命名空间,找到了一个更加可行而且方便应用的方式来动态添加控件并为其指定位置。
其实Canvas与Grid这些容器都有一些静态方法来实现通过代码定义控件的位置。比如对于Canvas而言,Canvas.SetLeft(UIElement element,double length)与Canvas.SetTop(UIElement element,double length)两个方法可以方便的将我们定义好的控件指定位置(绝对定位)。
还是上一次的xaml:
<UserControl x:Class="Test.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800" Height="600">
<Canvas x:Name="LayoutRoot" Background="Olive" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown">
</Canvas>
</UserControl>
将它的cs文件中的代码略作修改:
static int i = 0;
static int j = 0;
static bool canAdd = true;
private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (canAdd)
{
Button button = new Button();
button.Width = 70;
button.Height = 30;
button.Content = "Button" + (j * 8 + i).ToString();
Canvas.SetLeft(button, i * 100);
Canvas.SetTop(button, j * 50);
this.LayoutRoot.Children.Add(button);
if ((i + 1) * 100 < 800)
i++;
else
{
j++;
i = 0;
}
if (j * 50 >= 600)
canAdd = false;
}
else
{
this.LayoutRoot.Children.Clear();
i = 0;
j = 0;
canAdd = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
这段代码和上一次的,只是用Canvas.SetTop和Canvas.SetLeft实现了平移操作所做的事情而已。
运行效果如图,和上一次是一样的:
![](https://www.cnblogs.com/images/cnblogs_com/wodehuajianrui/silverlight002.jpg)
个人以为这样做比上一次更好一些。尽管控件并没有从Canvas中继承Canvas.Top、Canvas.Left属性,但是Canvas类提供的静态方法能让我们实现相应的功能。
其实Canvas与Grid这些容器都有一些静态方法来实现通过代码定义控件的位置。比如对于Canvas而言,Canvas.SetLeft(UIElement element,double length)与Canvas.SetTop(UIElement element,double length)两个方法可以方便的将我们定义好的控件指定位置(绝对定位)。
还是上一次的xaml:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
将它的cs文件中的代码略作修改:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这段代码和上一次的,只是用Canvas.SetTop和Canvas.SetLeft实现了平移操作所做的事情而已。
运行效果如图,和上一次是一样的:
![](https://www.cnblogs.com/images/cnblogs_com/wodehuajianrui/silverlight002.jpg)
个人以为这样做比上一次更好一些。尽管控件并没有从Canvas中继承Canvas.Top、Canvas.Left属性,但是Canvas类提供的静态方法能让我们实现相应的功能。
---------------------------------------------------------------
缥缈落花街 月圆月缺 望峦山平川 雁返君未还 怆然晚春残 忆天上人间
缥缈落花街 月圆月缺 望峦山平川 雁返君未还 怆然晚春残 忆天上人间