5.silverlight 动态创建按钮(三)
目的
使用NEW创建页面的控件,并且定位到相应的位置。
//一下代码来自网络
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication10
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
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();
TranslateTransform myTranslateTransform = new TranslateTransform();
myTranslateTransform.X = i * 100;//设置X 坐标
myTranslateTransform.Y = j * 50;//设置y坐标
button.RenderTransform = myTranslateTransform;
this.LayoutRoot.Children.Add(button);
if ((i + 1) * 100 < 800)
i++;
else
{
j++;
i = 0;
}
if (j * 50 >= 600)
canAdd = false;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication10
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
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();
TranslateTransform myTranslateTransform = new TranslateTransform();
myTranslateTransform.X = i * 100;//设置X 坐标
myTranslateTransform.Y = j * 50;//设置y坐标
button.RenderTransform = myTranslateTransform;
this.LayoutRoot.Children.Add(button);
if ((i + 1) * 100 < 800)
i++;
else
{
j++;
i = 0;
}
if (j * 50 >= 600)
canAdd = false;
}
}
}
}
<UserControl x:Class="SilverlightApplication10.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="blue" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown">
</Canvas>
</UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="blue" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown">
</Canvas>
</UserControl>
总结:
TranslateTransform是干什么的
在二维 x-y 坐标系内平移(移动)对象。
1.在Canvas中,直接使用Canvas.Top与Canvas.Left属性设置里面的控件位置
2.可以使用TextBlock.RenderTransform的TranslateTransform的X与Y属性来控制文本显示的位置。