none
Grid如果不设置Background属性,则不能接收拖放的对象? RRS feed

  • 问题

  • 前台代码:

    <Window x:Class="FirstWPF.PuzzleDemo"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="PuzzleDemo" Height="350" Width="525">
        <Grid>
        <Grid Name="grid1" Margin="0,35,257,26" ShowGridLines="True" MouseDown="grid1_MouseDown_1">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
     
            </Grid.ColumnDefinitions>
            <Image Stretch="Fill" Name="imag1" Source="images\first.jpg" Grid.Column="1" Grid.Row="1"/>
        </Grid>
        <Grid Name="grid2" Margin="252,35,0,26" Background="BlanchedAlmond" ShowGridLines="True" AllowDrop="True" Drop="grid2_Drop">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
        </Grid>
        </Grid>
    </Window>
    

    后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    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 FirstWPF
    {
        /// <summary>
        /// PuzzleDemo.xaml 的交互逻辑
        /// </summary>
        public partial class PuzzleDemo : Window
        {
            public PuzzleDemo()
            {
                InitializeComponent();
            }
     
     
            private void grid2_Drop(object senderDragEventArgs e)
            {
                if (e.Data.GetDataPresent(typeof(Image)))
                {
                    Image img = e.Data.GetData(typeof(Image)) as Image;
                    if (img.Name == "imag1")
                    {
                        grid1.Children.Remove(img);
                        Grid.SetColumn(img0);
                        Grid.SetRow(img0);
                        grid2.Children.Add(img);
     
                    }
                }
            }
     
            private void grid1_MouseDown_1(object senderMouseButtonEventArgs e)
            {
                if (imag1.IsMouseOver)
                {
                    DragDrop.DoDragDrop(grid1imag1, DragDropEffects.Move);
                }
            }
     
     
        }
    }
    

    如果grid2不设置Background属性,则不能接收拖放过来的图像?为什么?

    2013年10月30日 4:36

答案

全部回复