简体   繁体   English

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

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

在此处输入图片说明

I have a 333x 344 pixel image that I am displaying in WPF and it always displays the same size irrespective of the size of the window. 我有一个在WPF中显示的333x 344像素图像,并且无论窗口大小如何,它始终显示相同的大小。 Image is inside a dockpanel but changing dockpanel to grid didn't help. 图像在扩展面板内部,但是将扩展面板更改为网格没有帮助。

How can I get it in proportion to the size of the window? 如何获得与窗口大小成比例的图像?

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

or 要么

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

Edit: Below didn't fix the size. 编辑:下面没有固定大小。 DockPanel/Grid surrounding the image can be replaced with any other layout panels. 图像周围的DockPanel / Grid可以用任何其他布局面板替换。 Others cannot be changed. 其他不能更改。

img.Stretch = Stretch.None 

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

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

The default ItemsPanel for ItemsControl is a StackPanel , which restricts the space used by items in the direction of the "stack" (vertical by default). ItemsControl的默认ItemsPanelStackPanel ,它沿“堆栈”的方向(默认为垂直)限制项目使用的空间。 If you set the Stretch to Fill you would probably see it stretch to fill the horizontal space. 如果将“ Stretch设置为“ Fill ,则可能会看到它拉伸以填充水平空间。 A solution would be to change the panel template to a grid: 一种解决方案是将面板模板更改为网格:

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

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

But in this case it wouldn't make sense to use an ItemsControl at all because you are filling the whole area with a single item. 但是在这种情况下,根本不会使用ItemsControl ,因为您要用单个项目填充整个区域。 The UserControl is now the first item in the items collection and any additional items will be stacked on top it in a grid (unless you add row/column definitions to the panel template grid and define Grid.Row and Grid.Column on the items). UserControl现在是项集合中的第一项,所有其他项都将以网格的形式堆叠在其顶部(除非您将行/列定义添加到面板模板网格并在项上定义Grid.RowGrid.Column ) 。 Are you trying to create a background or container for list items? 您是否要为列表项创建背景或容器? In that case you should probably be defining the ItemsControl 's ControlTemplate instead. 在这种情况下,您可能应该改为定义ItemsControlControlTemplate

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

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