拖放功能不适用于自定义Window.Style

时间:2019-08-11 01:27:07

标签: c# wpf

我有一个使用wpf制作的简单UI。在其中包含一个包含TreeView的CustomControl的页面。重新排序TreeNodes我正在使用wpf dragdrop。到目前为止,效果很好。现在,我正在使用WindowChrome来创建无边界窗口。但是问题是:现在拖放无效。如果我尝试拖动对象,则光标会变为“无效放置位置”

我发现的内容:如果我删除自定义的通用类作品。但是我现在知道让拖放功能与我的自定义样式一起使用会缺少什么。

TreeView:

    <TreeView x:Name="StructureTree"
                  Grid.Row="0"
                  Padding="10,20,20,0"
                  VerticalAlignment="Stretch"
                  HorizontalAlignment="Stretch" 
                  ItemsSource="{Binding Path=RootElement.Children}"
                  Background="{x:Null}" 
                  BorderBrush="{x:Null}"
                  dd:DragDrop.IsDragSource="true"
                  dd:DragDrop.IsDropTarget="true"
                  dd:DragDrop.UseDefaultDragAdorner="true">

Window.Resources:

    <Window.Resources>
    <Style TargetType="{x:Type local:MainWindow}" BasedOn="{StaticResource {x:Type Window}}">
     <Setter Property="Template">
                <Setter.Value>
                  <ControlTemplate TargetType="{x:Type Window}">

                        <!-- Outer border with the dropshadow margin -->
                        <Border Padding="{Binding OuterMargin, FallbackValue=10}">

                            <!-- Main window outline -->
                            <Grid>

                                <!-- Opacity mask for corners on grid -->
                                <Border x:Name="Container"
                                        Background="{StaticResource BackgroundVeryLightBrush}"
                                        CornerRadius="{Binding WindowCornerRadius}" />

                                <!-- Window border and dropshadown -->
                                <Border CornerRadius="{Binding WindowCornerRadius}"
                                    Background="{Binding BackgroundVeryLightBrush}" BorderBrush="#FF1E1E1E">
                                    <Border.Effect>
                                        <DropShadowEffect ShadowDepth="2" Opacity="0.2" BlurRadius="5" />
                                    </Border.Effect>
                                    <Border.OpacityMask>
                                        <VisualBrush Visual="{Binding ElementName=Container}" />
                                    </Border.OpacityMask>
                                </Border>

                                <!-- The main window content -->
                                <Grid>

                                    <!-- Corner clipping -->
                                    <Grid.OpacityMask>
                                        <VisualBrush Visual="{Binding ElementName=Container}" />
                                    </Grid.OpacityMask>

                                    <Grid.RowDefinitions>

                                        <!-- Title Bar -->
                                        <RowDefinition Height="{Binding TitleHeight, FallbackValue=42}"/>
                                        <!-- Window Content -->
                                        <RowDefinition Height="*"/>

                                    </Grid.RowDefinitions>

                                    <!-- Title Bar -->
                                    <Grid Grid.Column="0" Grid.Row="0" Panel.ZIndex="1">
                                        <Grid.ColumnDefinitions>

                                            <!-- Icon -->
                                            <ColumnDefinition Width="Auto"/>
                                            <!-- Title -->
                                            <ColumnDefinition Width="*"/>
                                            <!-- Window Buttons -->
                                            <ColumnDefinition Width="Auto"/>

                                        </Grid.ColumnDefinitions>

                                        <!-- Icon -->
                                        <Button Margin="1" Padding="0" Style="{StaticResource IconButton}" WindowChrome.IsHitTestVisibleInChrome="True" Command="{Binding MenuCommand}">
                                            <!--<Image Source="/Images/Logo/Icon.ico"/>-->
                                        </Button>

                                        <!-- Title -->
                                        <Viewbox Grid.Column="0" Grid.ColumnSpan="3" Margin="0">
                                            <TextBlock Style="{StaticResource TitleText}"                                              
                                                       Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title, FallbackValue= 'Wellcome'}"/>
                                        </Viewbox>

                                        <!-- Window Buttons -->
                                        <StackPanel Grid.Column="2" Orientation="Horizontal">
                                            <Button Command="{Binding MinimizeCommand}" Style="{StaticResource WindowControlButton}" Content="_"/>
                                            <Button Command="{Binding MaximizeCommand}" Style="{StaticResource WindowControlButton}" Content="[ ]"/>
                                            <Button Command="{Binding CloseCommand}" Style="{StaticResource WindowCloseButton}" Content="X"/>
                                        </StackPanel>

                                    </Grid>
                                    <!-- Page Content -->
                                    <Border Grid.Row="1" Padding="{Binding InnerContentPadding}">
                                        <ContentPresenter   Content="{TemplateBinding Content}"/>
                                    </Border>
                                </Grid>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

1 个答案:

答案 0 :(得分:0)

当我尝试类似的情况时,我发现WPF如果无法检测到光标下的控件,将不允许放置该项目。解决此问题的方法可能是在TreeView“下方”添加透明填充的拉伸矩形:

    <Rectangle
        Fill="Transparent"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"/>
    <TreeView x:Name="StructureTree"...

只需将控件放在您的光标下,然后您就应返回有效的放置区域。

编辑:

如果它不起作用,请尝试将矩形放在模板中此部分的前面:

<!-- Opacity mask for corners on grid -->
<Border x:Name="Container"
        Background="{StaticResource BackgroundVeryLightBrush}"
        CornerRadius="{Binding WindowCornerRadius}" />

并且由于基本的窗口模板使用了AdornerDecorator,因此在需要的地方也值得一试:

<AdornerDecorator>
    <ContentPresenter.../>
</AdornerDecorator>