在DataGrid上为DragEnter执行事件触发器,导致XAML异常

时间:2013-11-17 16:23:09

标签: c# wpf xaml datagrid

我正在开发一个c#wpf项目,我遇到了与在XAML中触发触发器有关的问题。

我想要实现的是当将文件拖入网格时,它应该为背景颜色更改设置动画,但由于某种原因,它会在我运行程序时继续抛出异常。我收到以下错误:

  

'提供价值   'System.Windows.Baml2006.TypeConverterMarkupExtension'扔了一个   例外。'行号'9'和行位置'14'。

以下是XAML代码

<UserControl x:Class="ReportReader.UserControls.ReportDragDropControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
    <Grid AllowDrop="True" DragDrop.DragOver="Grid_DragOver"
          DragDrop.DragEnter="Grid_DragEnter" DragDrop.Drop="Grid_Drop" DragDrop.DragLeave="Grid_DragLeave">
        <Grid.Triggers>
            <EventTrigger RoutedEvent="Grid.DragEnter">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation To="#cecece" Storyboard.TargetProperty="(Grid.BackgroundColor).(SolidColorBrush.Color)" FillBehavior="Stop" Duration="0.0.1" />    
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Grid.Triggers>
        <TextBlock Margin="12,12,20,12" Name="txtDragDropStatus" Text="Drag file here or use file menu to load your report" TextAlignment="Center" FontSize="30" FontWeight="Bold" TextWrapping="WrapWithOverflow" Width="835" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</UserControl>

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

您的XAML文件中有两个错误 -

  1. BackgroundColor不是依赖属性,而是使用Background
  2. 0.0.1不是TimeSpan的有效值。它应该是0:0:1
  3. 这样可以正常使用 -

    <ColorAnimation To="#cecece"
                    Storyboard.TargetProperty="(Grid.Background)
                                                .(SolidColorBrush.Color)"
                    FillBehavior="Stop" Duration="0:0:1" />
    

    此外,要在background属性上允许动画,您应该将其设置为某个默认值。

    <Grid Background="White"/>
    

    我尝试使用此示例,并且在拖动输入时工作正常 -

    <Grid AllowDrop="True" Background="White" DragDrop.DragEnter="Grid_DragEnter">
        <Grid.Triggers>
            <EventTrigger RoutedEvent="Grid.DragEnter">
                <BeginStoryboard>
                    <Storyboard>
                       <ColorAnimation To="#cecece"
                                       Storyboard.TargetProperty="(Grid.Background).
                                                         (SolidColorBrush.Color)" 
                                       FillBehavior="Stop" Duration="0:0:1" />
                     </Storyboard>
                </BeginStoryboard>
             /EventTrigger>
         </Grid.Triggers>
         <TextBlock Margin="12,12,20,12" Name="txtDragDropStatus"
                    Text="Drag file here or use file menu to load your report"/>
    </Grid>