在WPF中拖放DataGrid行(可视反馈)

时间:2018-09-10 10:57:41

标签: wpf

如何在数据网格(而不是鼠标光标)的放置位置显示视觉反馈(红线)。

预先感谢

1 个答案:

答案 0 :(得分:0)

此代码应为您提供帮助。

<Style TargetType="{x:Type local:CustomDataGridRow}" x:Key="DataGridRowStyle" BasedOn="{StaticResource {x:Type DataGridRow}}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomDataGridRow}">
                        <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <SelectiveScrollingGrid>
                                <SelectiveScrollingGrid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </SelectiveScrollingGrid.ColumnDefinitions>
                                <SelectiveScrollingGrid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                </SelectiveScrollingGrid.RowDefinitions>
                                <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                                <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                <Rectangle Grid.Column="1" x:Name="Rect1" Stroke="Red" Fill="Red" Height="1" VerticalAlignment="Top" Visibility="Collapsed" IsHitTestVisible="False"/>
                                <Rectangle Grid.Column="1" x:Name="Rect2" Stroke="Red" Fill="Red" Height="1" VerticalAlignment="Bottom" Visibility="Collapsed" IsHitTestVisible="False"/>
                            </SelectiveScrollingGrid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="DragPosition" Value="{x:Static local:DragPosition.Up}">
                                <Setter TargetName="Rect1" Property="Visibility" Value="{x:Static Visibility.Visible}"/>
                            </Trigger>
                            <Trigger Property="DragPosition" Value="{x:Static local:DragPosition.Down}">
                                <Setter TargetName="Rect2" Property="Visibility" Value="{x:Static Visibility.Visible}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

<local:CustomDataGrid Grid.Column="1" RowStyle="{StaticResource DataGridRowStyle}" AllowDrop="True"/>

    public class CustomDataGrid : DataGrid
    {
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new CustomDataGridRow();
        }
    }

    public enum DragPosition { None, Up, Down }
    public class CustomDataGridRow : DataGridRow
    {
        public DragPosition DragPosition
        {
            get { return (DragPosition)GetValue(DragPositionProperty); }
            set { SetValue(DragPositionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DragPosition.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DragPositionProperty =
            DependencyProperty.Register("DragPosition", typeof(DragPosition), typeof(CustomDataGridRow), new FrameworkPropertyMetadata(DragPosition.None, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        protected override void OnDragOver(DragEventArgs e)
        {
            base.OnDragOver(e);
            if (e.GetPosition(this).Y > this.ActualHeight / 2)
                DragPosition = DragPosition.Down;
            else
                DragPosition = DragPosition.Up;
        }

        protected override void OnDragLeave(DragEventArgs e)
        {
            base.OnDragLeave(e);
            DragPosition = DragPosition.None;
        }
    }
相关问题