c# - 在 Windows 8 Metro App 中拖放图像

标签 c# wpf xaml windows-8

有没有办法在 Windows 8 Metro 应用程序中拖放图像。 我正在使用 C# 和 XAML。 以下是我需要的...

enter image description here

最佳答案

当然有。你必须自己控制它,但这很容易。您需要像这样使用一些指针事件:

XAML:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"   PointerMoved="GridPointerMoved">
    <Image x:Name="image1" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Source="Assets/imageFile.png"  PointerPressed="ImagePointerPressed" PointerReleased="ImagePointerReleased"/>
</Grid>

然后在你的 CS 文件中:

Point positionWithinImage;
private void ImagePointerPressed(object sender, PointerRoutedEventArgs e)
{
    Debug.WriteLine("Pressed");
    holding = true;

    positionWithinImage = e.GetCurrentPoint(sender as Image).Position;
}

private void ImagePointerReleased(object sender, PointerRoutedEventArgs e)
{
    Debug.WriteLine("Released");
    holding = false;
}

bool holding = false;

private void GridPointerMoved(object sender, PointerRoutedEventArgs e)
{
    if (holding)
    {
        var pos = e.GetCurrentPoint(image1.Parent as Grid).Position;
        image1.Margin = new Thickness(pos.X - this.positionWithinImage.X, pos.Y - this.positionWithinImage.Y, 0, 0);
    }
}

关于c# - 在 Windows 8 Metro App 中拖放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13000587/

相关文章:

c# - 使用 DateTime 值过滤 DataTable

c# - WPF/控制台混合应用程序

c# - 复选标记在菜单中显示为黑框

wpf - 将 FlowDocument 转换为简单文本

wpf - 在名称范围内找不到名称 ---在数据网格中显示动画时出错

c# - WPF - 通过相对源绑定(bind)为自定义标记扩展提供设计时值(value)

c# - 项目之间的列表框垂直间隙(删除??)

c# - 如何从 Java 迁移到 C#?

c# - 学习CSLA.NET框架

C# Visual Studio 2008 - Windows 窗体设计器中的设计时错误