繁体   English   中英

如何获得与窗口大小成比例显示的图像?

[英]How can I get images to display in proportion to the size of the window?

在此处输入图片说明

我有一个在WPF中显示的333x 344像素图像,并且无论窗口大小如何,它始终显示相同的大小。 图像在扩展面板内部,但是将扩展面板更改为网格没有帮助。

如何获得与窗口大小成比例的图像?

<Window>
<ScrollViewer>
<ItemSontrol>
<Usercontrol>
<Grid>
<DockPanel>
<Image/>
</DockPanel>
</Grid>
</Usercontrol>
</ItemSontrol>
</ScrollViewer>
</Window>

要么

<ItemSontrol>
<Usercontrol>
<Grid>
<Grid>
<Image/>
</Grid>
</Grid>
</Usercontrol>
</ItemSontrol>

编辑:下面没有固定大小。 图像周围的DockPanel / Grid可以用任何其他布局面板替换。 其他不能更改。

img.Stretch = Stretch.None 

将图像嵌入到ViewBox中 ,并确保其父级将其大小限制为相应的窗口(例如Grid

从xaml代码中的图像中删除宽度,高度,VerticalAligment和Horizo​​ntalAlignment属性。

ItemsControl的默认ItemsPanelStackPanel ,它沿“堆栈”的方向(默认为垂直)限制项目使用的空间。 如果将“ Stretch设置为“ Fill ,则可能会看到它拉伸以填充水平空间。 一种解决方案是将面板模板更改为网格:

<ItemsControl>
    <ItemsControl.ItemsPanelTemplate>
        <ItemsPanelTemplate>
            <Grid />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanelTemplate>

    <UserControl>
        ...
    </UserControl>
</ItemsControl>

但是在这种情况下,根本不会使用ItemsControl ,因为您要用单个项目填充整个区域。 UserControl现在是项集合中的第一项,所有其他项都将以网格的形式堆叠在其顶部(除非您将行/列定义添加到面板模板网格并在项上定义Grid.RowGrid.Column ) 。 您是否要为列表项创建背景或容器? 在这种情况下,您可能应该改为定义ItemsControlControlTemplate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM