C# 堆栈面板内的图像高度
我正在WPF中设计一个带有滚动查看器和堆栈面板的图像库,如下所示:C# 堆栈面板内的图像高度,c#,.net,wpf,C#,.net,Wpf,我正在WPF中设计一个带有滚动查看器和堆栈面板的图像库,如下所示: <ScrollViewer x:Name="ShopsScroll" Grid.Row="1" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"> <StackPanel x:Name="stackPanel" Margin="0" Orientation="Horizontal" Height
<ScrollViewer x:Name="ShopsScroll" Grid.Row="1" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
<StackPanel x:Name="stackPanel" Margin="0" Orientation="Horizontal" Height="Auto">
<Image Source="Images/1F/L1_angesb.jpg"/>
<Image Source="Images/1F/L1_chanel.jpg"/>
<Image Source="Images/1F/L1_dior.jpg"/>
<Image Source="Images/1F/L1_gucci.jpg"/>
<Image Source="Images/1F/L1_LV.jpg"/>
<Image Source="Images/1F/L1_nike.jpg"/>
</StackPanel>
由于图像的大小很大,我想调整它们的大小以适应StackPanel的高度。但是,当我在StackPanel中将height的值设置为“Auto”时,它只使用了图像的高度,而不是它的父ScrollViewer。如果我为StackPanel设置了一个固定值,这个问题似乎已经解决了,但是我需要在全屏的不同大小的屏幕中使用我的应用程序。因此,它应该适应不同的大小,而不是硬编码一个固定的值
如何操作?尝试为stackpanel设置
水平对齐=“拉伸”
和垂直对齐=“拉伸”
在我看来,最好在您的案例中使用带有图像的ItemsControl
<ItemsControl HorizontalContentAlignment="Stretch">
<Image Source="Images/1F/L1_angesb.jpg"/>
<Image Source="Images/1F/L1_chanel.jpg"/>
<Image Source="Images/1F/L1_dior.jpg"/>
<Image Source="Images/1F/L1_gucci.jpg"/>
<Image Source="Images/1F/L1_LV.jpg"/>
<Image Source="Images/1F/L1_nike.jpg"/>
</ItemsControl>
希望这有帮助您可以使用元素绑定将图像的高度绑定到StackPanel元素的高度。很抱歉,我现在无法测试此代码,但请尝试以下操作
<Image Source="Images/1F/L1_nike.jpg" Height="{Binding ElementName=ShopsScroll, Path=Height}" />
看
当您希望绑定到应用程序中另一个元素的属性时,此属性非常有用。例如,如果要使用滑块控制应用程序中另一个控件的高度,或者要将控件的内容绑定到ListBox控件的SelectedValue属性