ContentControl 旋转装饰器呈现

ContentControl Rotate decorator rendering

提问人:krajew4 提问时间:10/11/2012 最后编辑:David Makogonkrajew4 更新时间:12/5/2020 访问量:1150

问:

我最近偶然发现了以下问题:在我的 WPF 应用程序中,我实现了一个小设计器,您可以在其中将元素放在画布上、移动、缩放和旋转它们。

在搜索网络时,我发现了以下解决方案。这个解决方案实现了按类移动、缩放和旋转,所以我想我会根据我的应用程序调整这个解决方案并继续前进。问题是,虽然在我的机器上一切正常,但在其他机器上却存在一些渲染问题。我已经制作了我所说的内容的屏幕截图:System.Windows.Controls.Primitives.Thumb

screenshot

我正在使用 Windows 7,即使我在其他 Windows 7 上运行我的应用程序,它也呈现错误。我在计算机上使用 Windows XP 和其他兼容性设置运行我的应用程序,但我无法重现此错误。这是关于什么的,我可能做错了什么?

这是我用于内容控件样式的 xaml 文件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:COMPANY.WPUI.LayoutDesignModel.Thumbs">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="MoveThumb.xaml"/>
        <ResourceDictionary Source="ResizeDecorator.xaml"/>
        <ResourceDictionary Source="RotateDecorator.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="DesignerItemStyle" TargetType="ContentControl">
        <Setter Property="MinHeight" Value="50"/>
        <Setter Property="MinWidth" Value="50"/>
        <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                        <Control Name="RotateDecorator" Template="{StaticResource RotateDecoratorTemplate}" Visibility="Collapsed"/>
                        <s:MoveThumb Template="{StaticResource MoveThumbTemplate}" Cursor="SizeAll"/>
                        <Control x:Name="ResizeDecorator" Template="{StaticResource ResizeDecoratorTemplate}" Visibility="Collapsed"/>
                        <ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Selector.IsSelected" Value="True">
                            <Setter TargetName="ResizeDecorator" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="RotateDecorator" Property="Visibility" Value="Visible"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

这是 RotateDecorator.xaml 文件,恰好会导致问题:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:COMPANY.WPUI.LayoutDesignModel.Thumbs">

    <Style TargetType="{x:Type s:RotateThumb}">
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type s:RotateThumb}">
                    <Grid Width="30" Height="30">
                        <Ellipse Width="30" Height="30" Fill="#B0B0BB" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate x:Key="RotateDecoratorTemplate" TargetType="{x:Type Control}">
        <Grid>
            <s:RotateThumb Margin="-18,-18,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
            <s:RotateThumb Margin="0,-18,-18,0" VerticalAlignment="Top" HorizontalAlignment="Right" />
            <s:RotateThumb Margin="0,0,-18,-18" VerticalAlignment="Bottom" HorizontalAlignment="Right" />
            <s:RotateThumb Margin="-18,0,0,-18" VerticalAlignment="Bottom" HorizontalAlignment="Left" />
        </Grid>
    </ControlTemplate>
</ResourceDictionary>
C# .NET WPF

评论


答:

0赞 Panh 6/22/2015 #1

每当看到这样的东西时,我首先想到的是显卡。某些显卡可能会出现一些奇怪的行为,尤其是在它们的驱动程序未正确安装/更新的情况下。

0赞 Brandon 7/1/2015 #2

这是由 MergedDictionaries 引起的。“关系图设计器项目”将“移动”、“调整大小”和“旋转”操作拆分为 3 个单独的字典。从屏幕截图中,您可以看到已加载调整大小的拇指。就我而言,移动操作也有效,但就像问题一样,旋转拇指没有显示。没有抛出任何错误,但对 Snoop 的检查表明它无法找到旋转字典。

此解决方案扩展了我上面介绍的内容:https://stackoverflow.com/a/17083360/978622

解决方法:将资源字典合并到单个资源字典中。