在C#中调整WrapPanel内的图像大小 [英] Resizing Images within a WrapPanel in C#
问题描述
我正在尝试使用图像动态填充WrapPanel,
包含WrapPanel的窗口可以调整大小,我想
使图像内的图像当他们有更多的空间时,wrappanel调整大小。
我的问题是我希望图像调整大小如果我向wrappanel添加更多。
例如:
如果只有一个图像,它应该占用所有空间,
但是如果我添加另一个,它们都应该调整大小,这样两者都只占用一半的空间。
如果我调整窗口大小并使其成为两个图像应该增长更大,但是如果我缩小窗口,它们也应该缩小。
对不起我可怕的英语 -
我希望我能解释我的问题。
我的代码:
I'm trying to fill a WrapPanel dynamically with Images,
The Window that contains the WrapPanel is resizable, and i would like to
make the images within the wrappanel resize when they got more space.
My problem is that i want the images to resize if i add more to the wrappanel.
For example:
If there is only ONE image, it should take all the space it can,
but if i add another, they should both be resized, so that both only take half the space.
Both images should grow if i resize the Window and make it bigger, but they should also shrink if i shrink the Window.
I'm sorry for my terrible english-
I hope i could explain my problem.
My Code:
string path = s;
BitmapImage bit = new BitmapImage();
Uri ur = new Uri(path);
bit.BeginInit();
bit.UriSource = ur;
bit.EndInit();
System.Windows.Controls.Image tempimage = new System.Windows.Controls.Image();
tempimage.Source = bit;
tempimage.Stretch = Stretch.Uniform;
//### THESE DID NOT WORK
// tempimage.Height = Double.NaN;
//tempimage.Width = Double.NaN;
//wrapPanel1.ItemHeight = Double.NaN;
//wrapPanel1.ItemWidth = Double.NaN;
//###
wrapPanel1.Children.Add(tempimage);
有人能帮助我吗?
Can anybody help me?
推荐答案
Hi Feverbird,
只需使用UniformGrid,例如:
MainWindow.cs:
Hi Feverbird,
Just use UniformGrid instead, for example:
MainWindow.cs:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DockPanel LastChildFill="True">
<Border DockPanel.Dock="Top">
<Button Width="100" Content="Add" Click="Button_Click" />
</Border>
<UniformGrid x:Name="panel" />
</DockPanel>
</Window>
代码背后:
Code Behind:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var image = new BitmapImage(new Uri("Choice_toxicity_icon.png", UriKind.RelativeOrAbsolute));
panel.Children.Add(new Image() { Source = image , Stretch = Stretch.Fill});
}
}
最好的问候,
Shai
Best Regards,
Shai
这篇关于在C#中调整WrapPanel内的图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!