在Silverlight中,不为canvas / StackPanel触发事件

时间:2012-06-14 06:47:30

标签: c# silverlight drag-and-drop

XAML

 <Grid x:Name="LayoutRoot" Background="White">
        <Canvas  x:Name="VideoCanvas" Width="800" Height="600" AllowDrop="True" Background="RosyBrown" HorizontalAlignment="Center">
            <StackPanel Drop="VideoCanvas_Drop" AllowDrop="True" Name="CommandBar" Orientation="Horizontal" Background="Beige" Height="39" Width="783" Margin="5" HorizontalAlignment="Center" Canvas.Left="3">
                <Button Name="PlayMovie" Background="AntiqueWhite" Content="Play" FontWeight="Bold" Click="PlayMovie_Click" Margin="50,5,0,5" Width="100"/>
                <Button Name="PauseMovie" Background="AntiqueWhite" Content="Pause" FontWeight="Bold" Click="PauseMovie_Click" Margin="50,5,0,5" Width="100"/>
                <Button Name="VideoStop" Background="AntiqueWhite" Content="Stop" FontWeight="Bold" Click="VideoStop_Click" Margin="50,5,0,5" Width="100" />
            </StackPanel>
            <MediaElement x:Name="mediaEl" Canvas.Top="150" Width="800" Height="450" AllowDrop="True" AutoPlay="True" />
        </Canvas>
    </Grid>

C#

private void VideoCanvas_Drop(object sender, DragEventArgs e)
        {
            DragEventArgs dr = e as DragEventArgs;

            if (dr.Data == null)
                return;
            IDataObject data = dr.Data;
            FileInfo[] file = data.GetData(DataFormats.FileDrop) as FileInfo[];
            if (file.Count() > 0)
                mediaEl.SetSource(file[0].OpenRead());
            if (mediaEl.CurrentState != MediaElementState.Playing)
            {
                mediaEl.Play();
            }
        }

1 个答案:

答案 0 :(得分:0)

默认情况下,Silverlight中未实现拖放功能。使用Drag&amp;在Silverlight中删除功能,您必须使用Silverlight Toolkit中的DragDropTargets(PanelDragDropTarget,ListBoxDragDropTarget,...)。

<强> XAML

<Grid x:Name="LayoutRoot" Background="White">
        <Canvas  x:Name="VideoCanvas" Width="800" Height="600" AllowDrop="True" Background="RosyBrown" HorizontalAlignment="Center">
            <toolkit:PanelDragDropTarget AllowDrop="True" Drop="VideoCanvas_Drop">
              <StackPanel Name="CommandBar" Orientation="Horizontal" Background="Beige" Height="39" Width="783" Margin="5" HorizontalAlignment="Center" Canvas.Left="3">
                  <Button Name="PlayMovie" Background="AntiqueWhite" Content="Play" FontWeight="Bold" Click="PlayMovie_Click" Margin="50,5,0,5" Width="100"/>
                  <Button Name="PauseMovie" Background="AntiqueWhite" Content="Pause" FontWeight="Bold" Click="PauseMovie_Click" Margin="50,5,0,5" Width="100"/>
                  <Button Name="VideoStop" Background="AntiqueWhite" Content="Stop" FontWeight="Bold" Click="VideoStop_Click" Margin="50,5,0,5" Width="100" />
              </StackPanel>
            </toolkit:PanelDragDropTarget>
            <MediaElement x:Name="mediaEl" Canvas.Top="150" Width="800" Height="450" AllowDrop="True" AutoPlay="True" />
        </Canvas>
    </Grid>

C#

private void VideoCanvas_Drop(object sender, Microsoft.Windows.DragEventArgs e)
        {
            DragEventArgs dr = e as DragEventArgs;

            if (dr.Data == null)
                return;
            IDataObject data = dr.Data;
            FileInfo[] file = data.GetData(DataFormats.FileDrop) as FileInfo[];
            if (file.Count() > 0)
                mediaEl.SetSource(file[0].OpenRead());
            if (mediaEl.CurrentState != MediaElementState.Playing)
            {
                mediaEl.Play();
            }
        }