Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
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:
XML
<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:
C#
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));
            }
            
        }
    }
}


Thanks!

-Frank
Posted

1 solution

I'm an idiot!

I found the solutions just after asking this question:

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


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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900