[英]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和HorizontalAlignment屬性。
ItemsControl
的默認ItemsPanel是StackPanel
,它沿“堆棧”的方向(默認為垂直)限制項目使用的空間。 如果將“ Stretch
設置為“ Fill
,則可能會看到它拉伸以填充水平空間。 一種解決方案是將面板模板更改為網格:
<ItemsControl>
<ItemsControl.ItemsPanelTemplate>
<ItemsPanelTemplate>
<Grid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanelTemplate>
<UserControl>
...
</UserControl>
</ItemsControl>
但是在這種情況下,根本不會使用ItemsControl
,因為您要用單個項目填充整個區域。 UserControl
現在是項集合中的第一項,所有其他項都將以網格的形式堆疊在其頂部(除非您將行/列定義添加到面板模板網格並在項上定義Grid.Row
和Grid.Column
) 。 您是否要為列表項創建背景或容器? 在這種情況下,您可能應該改為定義ItemsControl
的ControlTemplate
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.