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;
            }
        }

    }
}
复制代码

 

 

 

 

复制代码
<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>
复制代码

 

 

 

总结:
TranslateTransform是干什么的
在二维 x-y 坐标系内平移(移动)对象。 

1.在Canvas中,直接使用Canvas.Top与Canvas.Left属性设置里面的控件位置
2.可以使用TextBlock.RenderTransform的TranslateTransform的X与Y属性来控制文本显示的位置。

 

 

 

posted @   红萝卜  阅读(1131)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示