2

我正在尝试将标签网格移动到鼠标的位置,就像冒险游戏中的移动一样。理想情况下,我会在途中删除并重绘它们。但是,现在我只想弄清楚如何将 int 转换为厚度或点到屏幕。

到目前为止,我有:

player.XMove =  (int)Mouse.GetPosition(Application.Current.MainWindow).X;
player.YMove = (int)Mouse.GetPosition(Application.Current.MainWindow).Y;

我尝试将 player.XMove 和 player.YMove 转换为厚度并将其存储为带有标签的网格的边距。我在转换时遇到了一些麻烦。那么,有没有人能够提供转换方面的帮助?此外,是否有更好的标签/网格属性将点转换为

编辑:添加了图片链接。

http://i1118.photobucket.com/albums/k608/sealclubberr/clickToMove_zps51f2359f.jpg

编辑:添加了当前代码和 XAML。我还没有机会摆弄它。

这是我当前的代码的样子:

public class User
    {
        private int fed;
        public int Fed
        {
            get
            {
                return fed;
            }
            set
            {
                fed = 5;
            }
        }

        private bool visible;
        public bool Visible
        {
            get
            {
                return visible;
            }
            set
            {
                visible = value;
            }
        }

        private int xMove;
        public int XMove
        {
            get
            {
                return xMove;
            }
            set
            {
                xMove = value;
            }
        }

        private int yMove;
        public int YMove
        {
            get
            {
                return yMove;
            }
            set
            {
                yMove = value;
            }
        }

    }

我最终会在用户类和位置类之间添加Has A或。(未发布。)Uses A

我在主窗口中的代码:

public partial class MainWindow : Window
    {
        User player = new User();
        ThicknessConverter perimeter = new ThicknessConverter();
        public MainWindow()
        {
            InitializeComponent();

        }

        private void Hansel_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
             Hansel.Visibility = Visibility.Visible;
        }

        private void Hansel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            player.XMove =  (int)Mouse.GetPosition(Application.Current.MainWindow).X;
            player.YMove = (int)Mouse.GetPosition(Application.Current.MainWindow).Y;

        }

        private void southRectangle_IsHitTestVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }


    }

我也在努力将布尔值转换为可见性,但遇到了 XAML 无法找到转换器命名空间的错误,该命名空间就在那里。

XAML 看起来像:

<Window x:Class="HanselAndGretalDisplay.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:HanselAndGretalDisplay"
        Title="MainWindow" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid>
        <Grid.Resources>
            <local:BoolToVisibleOrHidden x:Key="BoolToVisConverter"/>
        </Grid.Resources>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="179,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="79,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,119,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,19,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="698,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="698,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="698,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="723,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="623,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,119,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,19,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="179,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="79,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,450,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,350,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="623,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="623,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="623,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="698,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="598,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,450,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,350,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Grid x:Name="Hansel" IsVisibleChanged="Hansel_IsVisibleChanged" MouseLeftButtonDown="Hansel_MouseLeftButtonDown">
            <Label Content="Label" HorizontalAlignment="Left" Margin="398,218,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="398,243,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="398,268,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="373,243,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="423,243,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="413,293,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="383,293,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="439,268,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="353,268,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Rectangle x:Name="southRectangle" HorizontalAlignment="Left" Height="79" Margin="209,425,0,0" VerticalAlignment="Top" Width="383" IsHitTestVisibleChanged="southRectangle_IsHitTestVisibleChanged"/>
            <Rectangle x:Name="northRectangle" HorizontalAlignment="Left" Height="79" Margin="209,0,0,0" VerticalAlignment="Top" Width="412"/>
            <Rectangle x:Name="westRectangle" HorizontalAlignment="Left" Height="202" Margin="0,145,0,0" VerticalAlignment="Top" Width="133"/>
            <Rectangle x:Name="eastRectangle" HorizontalAlignment="Left" Height="202" Margin="627,145,0,0" VerticalAlignment="Top" Width="133"/>
        </Grid>
    </Grid>
</Window>

如果有一种简单的方法可以以编程方式生成 XAML 对象并将它们分配到理想的初始位置,那么从长远来看,这个项目是理想的,但现在我不太担心这一点。

4

1 回答 1

1
<Window x:Class="MiscSamples.ClickToMove"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ClickToMove" Height="300" Width="300">
    <ItemsControl ItemsSource="{Binding}" PreviewMouseDown="ItemsControl_PreviewMouseDown"
                  Background="#05FFFFFF">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Rectangle Stroke="Black" StrokeThickness="2" Fill="Blue"
                           Height="20" Width="20"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

代码背后:

  public partial class ClickToMove : Window
    {
        public List<MovableObject> Objects { get; set; } 

        public ClickToMove()
        {
            InitializeComponent();

            Objects = new List<MovableObject>
                {
                    new MovableObject() {X = 100, Y = 100}
                };

            DataContext = Objects;
        }

        private void ItemsControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var position = e.GetPosition(this);
            Objects.First().MoveToPosition(position.X, position.Y);
        }
    }

物品类别:

 public class MovableObject: INotifyPropertyChanged
    {
        private double _x;
        public double X
        {
            get { return _x; }
            set
            {
                _x = value;
                OnPropertyChanged("X");
            }
        }

        private double _y;
        public double Y
        {
            get { return _y; }
            set
            {
                _y = value;
                OnPropertyChanged("Y");
            }
        }

        private System.Threading.Timer MoveTimer;

        private double DestinationX;
        private double DestinationY;

        public void MoveToPosition(double x, double y)
        {
            DestinationX = x;
            DestinationY = y;

            if (MoveTimer != null)
                MoveTimer.Dispose();

            MoveTimer = new Timer(o => MoveStep(), null, 0, 10);
        }

        private void MoveStep()
        {
            if (Math.Abs(X - DestinationX) > 5)
            {
                if (X < DestinationX)
                    X = X+5;
                else if (X > DestinationX)
                    X = X-5;    
            }

            if (Math.Abs(Y - DestinationY) > 5)
            {
                if (Y < DestinationY)
                    Y = Y + 5;
                else if (Y > DestinationY)
                    Y = Y - 5;    
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                {
                    PropertyChangedEventHandler handler = PropertyChanged;
                    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));        
                }));

        }
    }
于 2013-04-07T19:27:08.110 回答