正确使用MouseDragElementBehavior

时间:2018-06-18 08:29:26

标签: c# wpf xaml

在我的MainWindow中,我定义了一个区域,我正在将我的UserControls注入(使用Prism,但这没关系)。我的控制应该是可调整大小(它完美地工作)和可移动的。要拖动和移动我的用户控件,我使用的是Blend的MouseDragElementBehavior。问题是,虽然我的控制移动,但当我调整MainWindow的大小时,它会被切断:

当我没有移动我的控件时,一切正常,当我调整MainWindow时,滚动条显示为:

enter image description here

但是当我将我的用户控件移动到MainWindow的底部时,ScrollBar没有显示出来,就像它不知道我的控件已被移动:

enter image description here

重要提示:ScrollBar在两种情况下都会在同一时间出现,但是当我向下移动用户控件时,当我到达用户控件的边界时,它应该显得多得多。在下面的图片中,您可以看到在我向下控制控制器后ScrollBar出现的那一刻。它似乎为时已晚,控制权被削减。 enter image description here

很难解释,如果你理解我,请告诉我。

我的代码:

<Window x:Class="ViewInjection.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        Title="MainWindow">
    <ScrollViewer HorizontalScrollBarVisibility="Auto"
        VerticalScrollBarVisibility="Auto">
        <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*" ScrollViewer.CanContentScroll="True" ScrollViewer.IsDeferredScrollingEnabled="True"/>
            </Grid.RowDefinitions>
            <Button Grid.Row="0" Click="Button_Click">Add View</Button>
            <ItemsControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Grid >
                        <Grid.RowDefinitions>
                                <RowDefinition   Height="*"/>
                        </Grid.RowDefinitions>                                               
                        </Grid>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Grid>
    </ScrollViewer>
</Window>

用户控件:

<UserControl
             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" 
             xmlns:local="clr-namespace:ViewInjection.Views"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" x:Class="ViewInjection.Views.ViewA"
             mc:Ignorable="d">   


    <Border  x:Name="Border" HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="#FF626161" BorderThickness="2" CornerRadius="3" MinHeight="200" MinWidth="200">
        <i:Interaction.Behaviors>
            <ei:MouseDragElementBehavior ConstrainToParentBounds="true"/>
        </i:Interaction.Behaviors>
        <DockPanel x:Name="sizableContent" Background="LightGray" Focusable="False" LastChildFill="True" MinHeight="300" MinWidth="300">


            <DockPanel DockPanel.Dock="Bottom" >
                <!--Make user control resizeable-->
                <Thumb DockPanel.Dock="Right" VerticalAlignment="Bottom" Margin="0,0,1,1"
                   DragDelta="OnResizeThumbDragDelta" 
                   DragStarted="OnResizeThumbDragStarted" 
                   DragCompleted="OnResizeThumbDragCompleted">
                    <Thumb.Style>
                        <Style TargetType="{x:Type Thumb}">
                            <Style.Setters>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate>
                                            <Grid x:Name="resizeVisual" DockPanel.Dock="Right" VerticalAlignment="Bottom"  >
                                                <Line X1="6" Y1="18" X2="18" Y2="6" Stroke="DarkGray" StrokeThickness="1.5"/>

                                                <Line X1="10" Y1="18" X2="18" Y2="10" Stroke="DarkGray" StrokeThickness="1.5"/>
                                                <Line X1="14" Y1="18" X2="18" Y2="14" Stroke="DarkGray" StrokeThickness="1.5"/>

                                                <Grid.Style>
                                                    <Style TargetType="{x:Type Grid}">
                                                        <Style.Triggers>
                                                            <Trigger Property="IsMouseOver" Value="True">
                                                                <Setter Property="Cursor" Value="SizeNWSE"/>
                                                            </Trigger>
                                                        </Style.Triggers>
                                                    </Style>
                                                </Grid.Style>
                                            </Grid>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style.Setters>
                        </Style>
                    </Thumb.Style>
                </Thumb>

                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                    <Button Margin="12" Width="75" TabIndex="1" Content="Ok"/>
                </StackPanel>
            </DockPanel>

            <StackPanel HorizontalAlignment="Center" Margin="16,16,16,4">
                <TextBlock Text="My UserControl"/>
            </StackPanel>
        </DockPanel>
    </Border>
</UserControl>

P.S。

我正在使用item控件在该区域内拥有多个控件。我使用Grid作为模板,因为我希望控件相互重叠。

0 个答案:

没有答案