1

このxamlを含むWPFウィンドウがあります。

<Window x:Class="WpfApplication2.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">

    <Window.Resources>
        <Style TargetType="Border" x:Key="BorderStyle">
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="1"/>
        </Style>
        <Style TargetType="Image" x:Key="ImageStyle">
            <Setter Property="Height" Value="75"/>
            <Setter Property="Width" Value="75"/>
            <Setter Property="AllowDrop" Value="True"/>
        </Style>
    </Window.Resources>
    <Grid>
        <GroupBox>
            <Grid>
                <ListBox x:Name="listbox">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" >
                                <TextBlock Text="{Binding}"/>
                                <Border Style="{StaticResource BorderStyle}">
                                    <Image Style="{StaticResource ImageStyle}" Drop="ThisDrop"/>
                                </Border>
                                <Border Style="{StaticResource BorderStyle}">
                                    <Image Style="{StaticResource ImageStyle}" Drop="ThatDrop"/>
                                </Border>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>
        </GroupBox>
    </Grid>
</Window>

そして、このコードの背後にあります:

using System.Linq;

namespace WpfApplication2
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            listbox.ItemsSource = Enumerable.Range(1, 3);
        }

        private void ThisDrop(object sender, System.Windows.DragEventArgs e)
        {
            // do something
        }

        private void ThatDrop(object sender, System.Windows.DragEventArgs e)
        {
            // do something else
        }
    }
}

Windowsエクスプローラーからイメージコントロールにファイルをドラッグアンドドロップすると、これらのドロップイベントを発生させるのに苦労しています。

プロジェクト: https ://github.com/ronnieoverby/WpfApplication2

4

4 に答える 4

1

問題は、画像制御ソースがまだ設定されていないことだったようです。

于 2012-09-11T18:51:49.460 に答える
0

基本的なドラッグアンドドロップ操作は次のとおりです。

private void Image_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        // Signal the user they can copy files here
        e.Effects = DragDropEffects.Copy;
        e.Handled = true;
    }
}

private void Image_Drop(object sender, DragEventArgs e)
{
    string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];

    if (fileList != null)
    {
        foreach (string file in fileList)
        {
            // Do stuff
        }

        e.Handled = true;
    }
}

私は通常、DragOverを実装し、DragEnterイベントが常に発生するとは限らない場合があるため、DragEnterでDragOverを呼び出すだけです。ただし、これはWindowsフォームを使用していたため、WPFでは必要ない場合があります。

DragDropEffectsを設定すると、2つのことが行われます。アイコンが変更されるため、ユーザーは何が起こるか(コピー、移動など)を視覚的に確認できます。また、呼び出し元(ドラッグドロップを開始したアプリケーション)に何が起こったかを通知するため、応答方法がわかります。たとえば、移動操作の場合、元のファイルが削除される可能性があります。

e.AllowedEffectsを使用して、呼び出し元アプリケーションがサポートする効果を確認します。たとえば、移動をサポートせず、コピーのみをサポートする場合があります。アプリケーションがリンク、移動、およびコピーの意味の規則を満たしていることを確認するのは、両端の開発者次第です。APIに関する限り、ユーザーに表示されるアイコンにのみ影響します。

于 2012-09-11T14:42:52.857 に答える
0

これを試して :

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!-- Using a StackPanel here since DataTemplate can't contain more than one element-->
            <StackPanel Orientation="Horizontal">
                <Image AllowDrop="True" Drop="ImageDrop1"/>
                <Image AllowDrop="True" Drop="ImageDrop2"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

そしてここにDropイベントハンドラーがあります

private void ImageDrop1(object sender, DragEventArgs e)
{
    MessageBox.Show("Dropped onto Image 1");
    e.Handled = true;
}

private void ImageDrop2(object sender, DragEventArgs e)
{
    MessageBox.Show("Dropped onto Image 2");
    e.Handled = true;
}
于 2012-09-11T15:31:48.323 に答える
0

たぶん、ICommandを受け取るAttachedPropertyを作成してから、(ビューモデルで)このコマンド内にコードを配置する必要があります。アタッチされたプロパティ内でOnDragイベントをサブスクライブし、ICommandを実行してから、ビュー(イメージ)でアタッチされたプロパティをコマンドにバインドします。

于 2012-09-11T20:08:43.857 に答える