zoukankan      html  css  js  c++  java
  • 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属性来控制文本显示的位置。

  • 相关阅读:
    MySQL 有关权限的表都有哪几个?
    MyISAM 表格将在哪里存储,并且还提供其存储格式?
    你是怎么看Spring框架的?
    elasticsearch 的倒排索引是什么 ?
    主键和候选键有什么区别?
    MySQL 支持事务吗?
    可以使用多少列创建索引?
    LIKE 声明中的%和_是什么意思?
    什么是通用 SQL 函数?
    MySQL 中有哪些不同的表格?
  • 原文地址:https://www.cnblogs.com/muer/p/1742745.html
Copyright © 2011-2022 走看看