Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中将元素动态定位到网格_C#_Wpf_Modern Ui - Fatal编程技术网

C# 在WPF中将元素动态定位到网格

C# 在WPF中将元素动态定位到网格,c#,wpf,modern-ui,C#,Wpf,Modern Ui,我正在使用ModernUI接口创建WPF应用程序。这是一种照片画廊。图像存储在某个文件夹中,并根据数据库中的相应记录进行检索。所以我的ViewModel从数据库获取信息,并将“URI”列绑定到图像的源属性 我需要做的是将这些图像定位到类似网格的视图中。图像的宽度和高度是恒定的。这里的挑战是,在运行之前,我不知道有多少元素,所以应该动态创建网格。若根据网格的宽度自动计算列的数量,我会更好。例如,图像宽度是200,右边距是50,所以如果网格(或者父元素,没关系)宽度是800,那么我们有3列。 但我可

我正在使用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>


这将水平添加
图像
控件,直到没有更多空间,然后将它们包装到下一行,依此类推。如果
图像
大小如您所说是恒定的,那么这应该会给您带来您想要的外观。当然,你需要将你的
图像
来源以正确的格式保存在收藏中。

这几乎是我想要的,而且它给了我一些新的想法。无论如何,非常感谢。这几乎是我想要的,而且它给了我一些新的想法。无论如何,非常感谢。这几乎是我想要的,而且它给了我一些新的想法。无论如何,非常感谢。这几乎是我想要的,而且它给了我一些新的想法。无论如何,非常感谢。