C# 在WPF中将元素动态定位到网格
我正在使用ModernUI接口创建WPF应用程序。这是一种照片画廊。图像存储在某个文件夹中,并根据数据库中的相应记录进行检索。所以我的ViewModel从数据库获取信息,并将“URI”列绑定到图像的源属性 我需要做的是将这些图像定位到类似网格的视图中。图像的宽度和高度是恒定的。这里的挑战是,在运行之前,我不知道有多少元素,所以应该动态创建网格。若根据网格的宽度自动计算列的数量,我会更好。例如,图像宽度是200,右边距是50,所以如果网格(或者父元素,没关系)宽度是800,那么我们有3列。 但我可以显式设置列数;最重要的是定位图像,使其看起来像网格C# 在WPF中将元素动态定位到网格,c#,wpf,modern-ui,C#,Wpf,Modern Ui,我正在使用ModernUI接口创建WPF应用程序。这是一种照片画廊。图像存储在某个文件夹中,并根据数据库中的相应记录进行检索。所以我的ViewModel从数据库获取信息,并将“URI”列绑定到图像的源属性 我需要做的是将这些图像定位到类似网格的视图中。图像的宽度和高度是恒定的。这里的挑战是,在运行之前,我不知道有多少元素,所以应该动态创建网格。若根据网格的宽度自动计算列的数量,我会更好。例如,图像宽度是200,右边距是50,所以如果网格(或者父元素,没关系)宽度是800,那么我们有3列。 但我可
ViewModel返回元素的ObservableCollection(可以更改为任何必要的结构)。我非常欣赏定义了模板的XAML代码。也许您可以尝试动态设置grid.column和grid.row属性。检查网格的可能宽度和高度,以指定可以放置的图片数量。然后定义网格的行和列并添加图像
for(amount of images) // define rows and colums
{
ColumnDefinition colDef = new ColumnDefinition();
colDef.Width = new GridLength(specifiedwidth);
yourgrid.ColumnDefinitions.Add(colDef);
RowDefinition rowDef = new RowDefinition();
rowDef.Height = new GridLength(specifiedheight);
yourgrid.RowDefinition.Add(rowDef);
}
for(amount of images) // add your images to the grid
{
yourgrid.Children.Add(yourimage);
Grid.SetColumn(yourimage, index); //set column index
Grid.SetRow(yourimage, index); // set row index
}
也许您可以尝试动态设置grid.column和grid.row属性。检查网格的可能宽度和高度,以指定可以放置的图片数量。然后定义网格的行和列并添加图像
for(amount of images) // define rows and colums
{
ColumnDefinition colDef = new ColumnDefinition();
colDef.Width = new GridLength(specifiedwidth);
yourgrid.ColumnDefinitions.Add(colDef);
RowDefinition rowDef = new RowDefinition();
rowDef.Height = new GridLength(specifiedheight);
yourgrid.RowDefinition.Add(rowDef);
}
for(amount of images) // add your images to the grid
{
yourgrid.Children.Add(yourimage);
Grid.SetColumn(yourimage, index); //set column index
Grid.SetRow(yourimage, index); // set row index
}
也许您可以尝试动态设置grid.column和grid.row属性。检查网格的可能宽度和高度,以指定可以放置的图片数量。然后定义网格的行和列并添加图像
for(amount of images) // define rows and colums
{
ColumnDefinition colDef = new ColumnDefinition();
colDef.Width = new GridLength(specifiedwidth);
yourgrid.ColumnDefinitions.Add(colDef);
RowDefinition rowDef = new RowDefinition();
rowDef.Height = new GridLength(specifiedheight);
yourgrid.RowDefinition.Add(rowDef);
}
for(amount of images) // add your images to the grid
{
yourgrid.Children.Add(yourimage);
Grid.SetColumn(yourimage, index); //set column index
Grid.SetRow(yourimage, index); // set row index
}
也许您可以尝试动态设置grid.column和grid.row属性。检查网格的可能宽度和高度,以指定可以放置的图片数量。然后定义网格的行和列并添加图像
for(amount of images) // define rows and colums
{
ColumnDefinition colDef = new ColumnDefinition();
colDef.Width = new GridLength(specifiedwidth);
yourgrid.ColumnDefinitions.Add(colDef);
RowDefinition rowDef = new RowDefinition();
rowDef.Height = new GridLength(specifiedheight);
yourgrid.RowDefinition.Add(rowDef);
}
for(amount of images) // add your images to the grid
{
yourgrid.Children.Add(yourimage);
Grid.SetColumn(yourimage, index); //set column index
Grid.SetRow(yourimage, index); // set row index
}
您只需将它们显示在一个
列表框
中,该列表框具有类型为WrapPanel
的ItemsPanelTemplate
:
<ListBox ItemsSource="{Binding ImageUrls}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<Image Source="{Binding}" Stretch="None" />
</ListBox.ItemTemplate>
</ListBox>
这将水平添加
图像
控件,直到没有更多空间,然后将它们包装到下一行,依此类推。如果图像
大小如您所说是恒定的,那么这应该会给您带来您想要的外观。当然,您需要在集合中使用正确格式的图像
源。您可以将它们显示在一个列表框
中,该列表框有一个ItemsPanelTemplate
类型的包装面板
:
<ListBox ItemsSource="{Binding ImageUrls}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<Image Source="{Binding}" Stretch="None" />
</ListBox.ItemTemplate>
</ListBox>
这将水平添加
图像
控件,直到没有更多空间,然后将它们包装到下一行,依此类推。如果图像
大小如您所说是恒定的,那么这应该会给您带来您想要的外观。当然,您需要在集合中使用正确格式的图像
源。您可以将它们显示在一个列表框
中,该列表框有一个ItemsPanelTemplate
类型的包装面板
:
<ListBox ItemsSource="{Binding ImageUrls}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<Image Source="{Binding}" Stretch="None" />
</ListBox.ItemTemplate>
</ListBox>
这将水平添加
图像
控件,直到没有更多空间,然后将它们包装到下一行,依此类推。如果图像
大小如您所说是恒定的,那么这应该会给您带来您想要的外观。当然,您需要在集合中使用正确格式的图像
源。您可以将它们显示在一个列表框
中,该列表框有一个ItemsPanelTemplate
类型的包装面板
:
<ListBox ItemsSource="{Binding ImageUrls}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<Image Source="{Binding}" Stretch="None" />
</ListBox.ItemTemplate>
</ListBox>
这将水平添加
图像
控件,直到没有更多空间,然后将它们包装到下一行,依此类推。如果图像
大小如您所说是恒定的,那么这应该会给您带来您想要的外观。当然,你需要将你的图像
来源以正确的格式保存在收藏中。这几乎是我想要的,而且它给了我一些新的想法。无论如何,非常感谢。这几乎是我想要的,而且它给了我一些新的想法。无论如何,非常感谢。这几乎是我想要的,而且它给了我一些新的想法。无论如何,非常感谢。这几乎是我想要的,而且它给了我一些新的想法。无论如何,非常感谢。