移动WPF'元素'? (C#) [英] Moving a WPF 'element'? (C#)

查看:86
本文介绍了移动WPF'元素'? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google上找到了一些有用的信息,我想在这里问:



如何从我的移动WPF元素(例如标签)运行代码?



这就是我所拥有的(它只是我用来测试的沙箱应用程序),代码方面,(注意一行有一个评论错误代码;这是我从网上示例中窃取的代码):



我的WPF:

I find little helpful information with google and thought I'd ask here:

How do I move a WPF element, (e. g. a lable), from my running code?

this is what I have, (it's just a sandbox application I use for testing), in terms of code, (notice that the one line has a commented errorcode; this is code I've stolen from examples on the net):

My WPF:

<Window x:Class="KeypressExperimental.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525" removed="#CCCCCC" KeyDown="Window_KeyDown">
    <Grid>
        <ListBox Height="287" HorizontalAlignment="Left" Margin="263,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="228" />
        <Canvas Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="canvas1" VerticalAlignment="Top" Width="245">
            <Label Content="Player" Height="28" Name="player" Background="#FF5DB15D" Width="45" Canvas.Left="93" Canvas.Top="125" />
        </Canvas>
    </Grid>
</Window>



我的代码:


My Code:

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.Navigation;
using System.Windows.Shapes;

namespace KeypressExperimental
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Up)
            {
                player.Margin.Top = 10; // Error:	"Cannot modify the return value of 'System.Windows.FrameworkElement.Margin' because it is not a variable"
            }
            else if (e.Key == Key.Down)
            {

            }
            else if (e.Key == Key.Left)
            {

            }
            else if (e.Key == Key.Right)
            {

            }
            else
            {
                listBox1.Items.Add(Convert.ToString(e.Key));
            }
            
        }
    }
}





谢谢!< br $>


-Frank



Thanks!

-Frank

推荐答案

我是个白痴!



我在问这个问题之后就找到了解决方案:



I'm an idiot!

I found the solutions just after asking this question:

player.Margin = new Thickness(player.Margin.Left,
                        player.Margin.Top - 10,
                        player.Margin.Right,
                        player.Margin.Bottom);





这会将名为player的标签向上移动10个像素。 br />


至少现在有一个很好的解决方案,因为厚度不是我与保证金属性直观相关的东西。



-Frank



This moves the label named "player" 10 pixels upwards.

At least there are s great solution for this now, as "thickness" wasn't something I intuitively associated with margin properties.

-Frank


这篇关于移动WPF'元素'? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆