2015-04-24 42 views
0

當用戶在其上拖動文件時,我正在嘗試更改BackgroundBorder使用XAML拖動效果

r

我想只定義使用XAML效果。

我在下面嘗試了,但在Border上拖動文件時Background未被更改。

<Border Name="dropBorder" BorderThickness="1" AllowDrop="True">  
    <Border.Triggers> 
     <EventTrigger RoutedEvent="DragOver"> 
       <BeginStoryboard>  
        <Storyboard Storyboard.TargetProperty="Background"> 
          <ColorAnimation From="Transparent" To="#FF444444" Duration="0:0:0.5" /> 
         </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Border.Triggers> 
     <TextBlock Text="Drag and drop file(s) here" Foreground="Gray" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10"/> 
</Border> 

我也試着如下使用DragEnter沒有結果

<EventTrigger RoutedEvent="Border.DragEnter"> 
     <BeginStoryboard> 
      <Storyboard> 
       <ColorAnimation Storyboard.TargetName="dropBorder" 
           Storyboard.TargetProperty="Background" 
           Duration="0:0:0.5" 
           From="Transparent" To="#FF444444"/> 
      </Storyboard> 
     </BeginStoryboard> 
</EventTrigger> 

回答

1

我沒有很滿足您100%的要求。我創建了一個附加屬性,我通過代碼隱藏來設置屬性,因此您需要對此進行評估。此外,移動顏色動畫,因爲你試圖刷動畫而不是顏色。

XAML:

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:wpfApplication1="clr-namespace:WpfApplication1" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <SolidColorBrush x:Key="SharedBackgroundBrush" Color="Transparent" /> 
    </Window.Resources> 
    <Border Name="dropBorder" BorderThickness="1" AllowDrop="True" DragEnter="DropBorder_OnDragEnter" DragLeave="DropBorder_OnPreviewDragLeave" Background="{StaticResource SharedBackgroundBrush}"> 
     <Border.Style> 
      <Style> 
       <Style.Triggers> 
        <Trigger Property="wpfApplication1:DragDropHelper.IsDragOver" Value="True"> 
         <Trigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard Storyboard.Target="{StaticResource SharedBackgroundBrush}" Storyboard.TargetProperty="Color"> 
            <ColorAnimation From="Transparent" To="Yellow" Duration="0:0:0.5" /> 
           </Storyboard> 
          </BeginStoryboard> 
         </Trigger.EnterActions> 
         <Trigger.ExitActions> 
          <BeginStoryboard> 
           <Storyboard Storyboard.Target="{StaticResource SharedBackgroundBrush}" Storyboard.TargetProperty="Color"> 
            <ColorAnimation From="Yellow" To="Transparent" Duration="0:0:0.5" /> 
           </Storyboard> 
          </BeginStoryboard> 
         </Trigger.ExitActions> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Border.Style> 
     <TextBlock Text="Drag and drop file(s) here" Foreground="Gray" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10"/> 
    </Border> 
</Window> 

代碼:

using System.Windows; 

namespace WpfApplication1 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void DropBorder_OnDragEnter(object sender, DragEventArgs e) 
     {    
      DragDropHelper.SetIsDragOver((DependencyObject)sender, true);   
     } 

     private void DropBorder_OnPreviewDragLeave(object sender, DragEventArgs e) 
     { 
      DragDropHelper.SetIsDragOver((DependencyObject)sender, false);   
     } 
    } 

    public class DragDropHelper 
    { 
     public static readonly DependencyProperty IsDragOverProperty = DependencyProperty.RegisterAttached(
      "IsDragOver", typeof (bool), typeof (DragDropHelper), new PropertyMetadata(default(bool))); 

     public static void SetIsDragOver(DependencyObject element, bool value) 
     { 
      element.SetValue(IsDragOverProperty, value); 
     } 

     public static bool GetIsDragOver(DependencyObject element) 
     { 
      return (bool) element.GetValue(IsDragOverProperty); 
     } 
    } 
}