none
WPF能否动态加载控件 RRS feed

  • 问题

  • 窗体上本没有Image控件
    点击button后
    希望在windows.xaml.cs通过button的click事件
    写代码让窗体上动态生成一个Image控件
    再显示出一张图片


    因为我是想点击一下button就显示出一张图片 图片还要是通过Image控件显示的
    因为我的拖拽事件已经做出来了

    求助老鸟西..
    这个东西怎么搞?
    2009年1月16日 11:20

答案

全部回复

  •  

    Image image = new Image(...)

    Grid.SetRow(image, 1);

    Grid.SetColumn(image. 2);

    mainGrid.Children.Add(image);

    2009年1月16日 13:15
  • 我的xaml
    <Window x:Class="MSDNimage.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MSDNimage" WindowState="Maximized"
        >
        <Window.Resources>
            <Style TargetType="Image">
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="Image_PreviewMouseLeftButtonDown" />
            </Style>
        </Window.Resources>

        <Grid>
            <Canvas Name="ButtonCanvas">
                <Button Name="btn1" Click="btn1_Click" Canvas.Left="100" Canvas.Top="100" Width="40" Height="40" >
                    <Image Source="buttonimages/buildingbutton.jpg"/>
                </Button>
                <Button Name="btn2" Click="btn2_Click" Canvas.Left="100" Canvas.Top="200" Width="40" Height="40">
                    <Image Source="buttonimages/treebutton.jpg"/>
                </Button> 
            </Canvas>
            <Canvas Name="ImageCanvas" DragOver="ImageCanvas_DragOver" AllowDrop="True"
                    PreviewMouseLeftButtonUp="ImageCanvas_PreviewMouseLeftButtonUp"
                    MouseMove="ImageCanvas_MouseMove" Margin="-8,0,8,0"  >
               
              <Image Name="img" Height="100" Width="100"  Canvas.Top="100" Canvas.Left="300" Cursor="Hand" ></Image>
                <Image  Name="img1" Height="100" Width="100"  Canvas.Top="300" Canvas.Left="300" Cursor="Hand" ></Image>
            </Canvas>
        </Grid>

    </Window>


    xaml.cs



    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    namespace MSDNimage
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>

        public partial class Window1 : System.Windows.Window
        {
           
          
           
            public Window1()
            {
                InitializeComponent();

                //btn1.Click +=new RoutedEventHandler(btn1_Click);
            }

            public void btn1_Click(object sender, RoutedEventArgs e)
            {
                    Image image = new Image();
                   
                    // 创建一个源
                    BitmapImage myBitmapImage = new BitmapImage();
                    // BitmapImage.UriSource必须使用BeginInit/EndInit块
                    myBitmapImage.BeginInit();//BitmapImage初始化开始
                    myBitmapImage.UriSource = new Uri(@"sampleImages/building.jpg", UriKind.Relative);//获取或设置 BitmapImage 的 Uri 源。这是一个依赖项属性。

                    myBitmapImage.EndInit();//表示 BitmapImage 初始化结束。
                    //把源赋给Image控件
                    img.Source = myBitmapImage;
                    
                  
                   
                   

                    
                  
            }
            public void btn2_Click(object sender, RoutedEventArgs e)
            {
                Image image = new Image();
                BitmapImage myBitmapImage1 = new BitmapImage();
                // BitmapImage.UriSource必须使用BeginInit/EndInit块
                myBitmapImage1.BeginInit();
                myBitmapImage1.UriSource = new Uri(@"sampleImages/梧桐树.jpg", UriKind.Relative);
                myBitmapImage1.EndInit();
                //把源赋给Image控件
                img1.Source = myBitmapImage1;
               
            }
       







            #region
            private bool _isDragging = false;
            private Point _startPoint;
            private Point _relativePoint;
            private Image _selectImage;

            private void Image_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                _selectImage = sender as Image;
                _startPoint = e.GetPosition(ImageCanvas);
                _relativePoint = e.GetPosition(_selectImage);
                _isDragging = true;

                foreach (Image image in ImageCanvas.Children)
                {
                    image.SetValue(Canvas.ZIndexProperty, 0);
                }
                _selectImage.SetValue(Canvas.ZIndexProperty, 1);
            }

            private void ImageCanvas_DragOver(object sender, DragEventArgs e)
            {
                if (e.Data != null && e.Data.GetDataPresent(typeof(Image)))
                {
                    Image image = e.Data.GetData(typeof(Image)) as Image;
                    Point p = e.GetPosition(ImageCanvas);

                    image.SetValue(Canvas.LeftProperty, p.X - _relativePoint.X);
                    image.SetValue(Canvas.TopProperty, p.Y - _relativePoint.Y);
                }
            }

            private void ImageCanvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                _isDragging = false;
            }

            private void ImageCanvas_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed && _isDragging)
                {
                    Point newPos = e.GetPosition(ImageCanvas);
                    if ((Math.Abs(newPos.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance) ||
                        (Math.Abs(newPos.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance))
                    {
                        ImageCanvas.GiveFeedback += new GiveFeedbackEventHandler(ImageCanvas_GiveFeedback);
                      
                        DragDrop.DoDragDrop(ImageCanvas, _selectImage, DragDropEffects.Move);                   
                       
                        _isDragging = false;   
                        ImageCanvas.GiveFeedback -= new GiveFeedbackEventHandler(ImageCanvas_GiveFeedback);
                    }
                }
            }
            void ImageCanvas_GiveFeedback(object sender, GiveFeedbackEventArgs e)
            {
                Mouse.SetCursor(Cursors.Hand);  //Set Cursor
                e.Handled = true;
            }
            #endregion

        }
    }
      




    应该怎么搞?
    麻烦给出完整的代码..
    不要就那一段 我看不懂
    我只是想在现在的基础上实现 点一下button 就出现一张图片


    然后在拖拽也能用现在这个代码


    求帮助啊!
    跪求!
    2009年1月17日 2:28