none
Win7触控拖动图片到StackPanel却没有显示 RRS feed

  • 问题

  • 正在做一个自己的小项目,如下图:

    目的是将Canvas中的小红圈拖动到下边的StackPanel里,类似字母排序游戏,但要使用触控技术来实现。我在代码中使用了操作事件,其他部分还好,但在StackPanel.Children.Add(someVar)语句后,StackPanel里并未显示someVar,诡异的是调试时StackPanel的Children数量确实增加了1……求论坛里老师给予解答,谢谢!全部代码如下。

    [前端Xaml]

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="650" Width="800">
        <Grid Name="mainFrame">
            <Grid.RowDefinitions>
                <RowDefinition Height="538*" />
                <RowDefinition Height="73*" />
            </Grid.RowDefinitions>
            <Canvas Grid.Row="0" Name="myCanvas"/>
            <StackPanel Grid.Row="1" Name="myStack" Orientation="Horizontal"/>
        </Grid>
    </Window>

    [后台CS]

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Shapes;

    namespace WpfApplication1
    {
        public partial class MainWindow : Window
        {
            Rectangle _Rect;
            Label _Label; //用来在运行时监视_Rect的视觉父元素

            public MainWindow()
            {
                InitializeComponent();
                this.Loaded+=new RoutedEventHandler(MainWindow_Loaded);
            }
           
            void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                _Rect = new Rectangle() {
                    Fill = new SolidColorBrush(Colors.BlanchedAlmond),
                    Width = 50,
                    Height = 50
                };
                _Rect.RenderTransform = new MatrixTransform(1, 0, 0, 1, 30, 30);
                _Rect.IsManipulationEnabled = true;
               
                _Rect.ManipulationStarting += (s, arg) => {
                    arg.ManipulationContainer = myCanvas;
                    arg.Handled = true;
                };               
                _Rect.ManipulationDelta+=
                    new EventHandler<ManipulationDeltaEventArgs>(_Rect_ManipulationDelta);
                myCanvas.Children.Add(_Rect);
               
                mainFrame.Children.Remove(myStack);
                Border border = new Border() {
                    BorderThickness  = new Thickness(3),
                    BorderBrush = new SolidColorBrush(Colors.Chocolate),
                    Child = myStack
                };
                mainFrame.Children.Add(border);
                Grid.SetRow(border, 1);

                _Label = new Label() { Content = ""};
                myCanvas.Children.Add(_Label);
                Canvas.SetLeft(_Label, 5);
                Canvas.SetTop(_Label, 5);
                Canvas.SetZIndex(_Label, 0);
            }     

            void _Rect_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
            {
                double x, y;
                Point currentPosition = e.ManipulationOrigin;
                Rectangle rect = e.Source as Rectangle;
                MatrixTransform xform = rect.RenderTransform as MatrixTransform;
                Matrix matrix = xform.Matrix;
                ManipulationDelta delta = e.DeltaManipulation;

                x = delta.Translation.X;
                y = delta.Translation.Y;
                if (currentPosition.Y + rect.ActualHeight / 2 > myCanvas.ActualHeight)
                {
                    myCanvas.Children.Remove(rect);
                    myStack.Children.Add(rect);
                   
                    e.Complete();
                }
                else
                {
                    if (currentPosition.X - rect.ActualWidth / 2 < 0 ||
                    currentPosition.X + rect.ActualWidth / 2 > myCanvas.ActualWidth)
                    {
                        x = 0;
                    }
                    if (currentPosition.Y - rect.ActualHeight / 2 < 0)
                    {
                        y = 0;
                    }
                    delta = new ManipulationDelta(new Vector(x, y), 0, new Vector(), new Vector());
                    matrix.Translate(delta.Translation.X, delta.Translation.Y);
                    xform.Matrix = matrix;
                }

                _Label.Content = rect.Parent.ToString();
                e.Handled = true;
            }
        }
    }

    2012年9月13日 3:19

答案

  • 看你的代码中用的是RenderTransform ,我猜想,你的确移动进去了。但是呈现的时候已经在Window的边界外了。所以看不到。

    这个非常好测试我的推断。 你把StackPanle的大小调整到Canva的2倍试试。应该就能看到了。

    2012年9月14日 9:29