Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF c中将图像和矩形组合在一起#_C#_Wpf_Xaml - Fatal编程技术网

C# 在WPF c中将图像和矩形组合在一起#

C# 在WPF c中将图像和矩形组合在一起#,c#,wpf,xaml,C#,Wpf,Xaml,我的问题是我有一个代表平面的图像,还有一个代表被捕获并保存为图像的区域的矩形。然而,当它们在WPF周围移动时,它们以不同的速度移动,即使它们被告知以相同的速度移动 是否有一种方法可以将它们组合在某个标记中,以便它们被视为一个对象,但同时又是分开的?我需要它们分开,因为矩形的大小应该能够改变,而平面的图像不应该改变 xaml: 事先非常感谢就像Chris W.提到的那样:把它们扔到格子里就行了 我建议您在使用WPF时尝试移动MVVM模式,并冒昧地编写了一个小示例。为移动项目(包含图像和矩形的网格)

我的问题是我有一个代表平面的图像,还有一个代表被捕获并保存为图像的区域的矩形。然而,当它们在WPF周围移动时,它们以不同的速度移动,即使它们被告知以相同的速度移动

是否有一种方法可以将它们组合在某个标记中,以便它们被视为一个对象,但同时又是分开的?我需要它们分开,因为矩形的大小应该能够改变,而平面的图像不应该改变

xaml:


事先非常感谢

就像Chris W.提到的那样:把它们扔到格子里就行了

我建议您在使用WPF时尝试移动MVVM模式,并冒昧地编写了一个小示例。为移动项目(包含图像和矩形的网格)定义viewmodel

这里我使用RelayCommand类将用户输入路由到viewmodel。您可以阅读有关RelayCommand的更多信息。INotifyPropertyChanged的实现是为了通知视图视图模型中的更改

在您的视图(可能只是主窗口)中,通过键绑定调用MoveRight方法。栅格的位置绑定到viewmodel的边距特性

<Window.InputBindings>
    <KeyBinding Command="{Binding Path=MoveRightCommand}" 
  Key="Right" ></KeyBinding>
</Window.InputBindings>
<Grid>
    <Grid Margin="{Binding Thickness}" Height="100" Width="100" Background="Blue">
        <Label Content="I like to move it!" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="100"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="0,41,0,0" TextWrapping="Wrap" Text="Move it!" VerticalAlignment="Top" Width="100"/>
    </Grid>
</Grid>

这不仅是为了回答您的问题(我希望是这样),也是为了让您快速了解MVVM。在网上可以找到大量更深入的信息

只需将它们嵌入
网格中
if ((Keyboard.GetKeyStates(Key.A) & KeyStates.Down) > 0)
        {
            Console.WriteLine("Test left key is pressed ");
            rect.Margin = new Thickness(rect.Margin.Left -10 , rect.Margin.Top, rect.Margin.Right, rect.Margin.Bottom);
            planeIMG.Margin = new Thickness(planeIMG.Margin.Left -10, planeIMG.Margin.Top, planeIMG.Margin.Right, planeIMG.Margin.Bottom);
        }
 class MovingItemViewModel : INotifyPropertyChanged 
{
    public Thickness Margin { get; set; }
    public String Data { get; set; }

    public ICommand MoveRightCommand { get; set; }

    public MovingItemViewModel() {
        Margin = new Thickness(0, 0, 0, 0);
        Data = "Hello!";
        MoveRightCommand = new RelayCommand(param => MoveRight());
    }

    public void MoveRight() {
        Console.WriteLine("Right Key is pressed!");
        double offset = Margin.Right - 10;
        Margin = new Thickness(0, 0, offset, 0);
        OnPropertyChanged("Margin");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged( [CallerMemberName] string propertyName = null ) {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
<Window.InputBindings>
    <KeyBinding Command="{Binding Path=MoveRightCommand}" 
  Key="Right" ></KeyBinding>
</Window.InputBindings>
<Grid>
    <Grid Margin="{Binding Thickness}" Height="100" Width="100" Background="Blue">
        <Label Content="I like to move it!" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="100"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="0,41,0,0" TextWrapping="Wrap" Text="Move it!" VerticalAlignment="Top" Width="100"/>
    </Grid>
</Grid>
 public MainWindow()
    {
        InitializeComponent();
        MovingItemViewModel vm = new MovingItemViewModel();
        DataContext = vm;
    }