从ViewBox拉伸中免除AdornerLayer中的控制

时间:2013-05-09 18:15:52

标签: .net wpf c#-4.0

我有一个大致如下所示的WPF控件:

<ViewBox Stretch="Uniform" Name="viewboxName">
    <ItemsControl>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <!-- a bunch of controls here that I want stretched in the viewbox -->
             </DataTemplate>
         </ItemsControl.ItemTemplate>
    </ItemsControl>
</ViewBox>

然后,在AdornerLayer中,我(使用基于http://shevaspace.blogspot.com/2007/02/visual-level-programming-vs-logical.html的技术)定义为

的按钮控件
<Button>
    <Image Source="/AcchImageLoad;component/icons/metroStudio/ImageRotation.png" Stretch="None" />
</Button>

如何在AdornerLayer中使用此按钮来使用图像的原始分辨率,而不是使用ViewBox进行拉伸?

2 个答案:

答案 0 :(得分:1)

基本上,我们的想法是从ViewBox获得转换并使用绑定转换器应用逆转。

绑定转换器可以使用以下代码获得反转:

((ContainerVisual)VisualTreeHelper
.GetChild((System.Windows.DependencyObject)viewbox, 0)).Transform.Inverse

在xaml中,绑定看起来像

<Button.LayoutTransform>
    <MultiBinding Converter="{bc:ExemptFromViewboxTransformConverter}">
       <Binding Source="{x:Reference Name=viewboxName}" />
       <!-- The ActualWidth/ActualHeight bindings are only used to ensure 
            the transform updates when the window is resized. -->
        <Binding Source="{x:Reference Name=viewboxName}" Path="ActualWidth" />
        <Binding Source="{x:Reference Name=viewboxName}" Path="ActualHeight" />
    </MultiBinding>
</Button.LayoutTransform>

另见:

答案 1 :(得分:0)

您可以使用此解决方案禁用视窗框缩放:Disable Viewbox Resize for Specific Elements

基本上,为ViewBox_SizeChanged添加一个处理程序,并将exempt元素的变换设置为ViewBox变换的反转。

然后在您的XAML中添加ViewBoxExtra.DisableScaling =&#34; true&#34;,例如

<Rectangle Width="50" Height="50" local:ViewBoxExtra.DisableScaling="true"/>
相关问题