Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.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_Image_Grid - Fatal编程技术网

C# 使用WPF在网格中显示图像

C# 使用WPF在网格中显示图像,c#,wpf,image,grid,C#,Wpf,Image,Grid,我正在创建一个应用程序,里面有一个存储区,所以我需要一个网格视图来显示带有文本的项目图标。iTunes为我提供了一个很好的例子。有什么想法吗 您可以使用一个面板类型为WrapPanel的列表框,然后使用一个数据模板,该模板使用图像元素作为图标,使用文本块作为标题 例如: 在window.xaml.cs中: public List<MyItemType> MyItems { get; set; } public Window1() { InitializeComponent(

我正在创建一个应用程序,里面有一个存储区,所以我需要一个网格视图来显示带有文本的项目图标。iTunes为我提供了一个很好的例子。有什么想法吗


您可以使用一个面板类型为
WrapPanel
列表框,然后使用一个数据模板,该模板使用
图像元素作为图标,使用文本块作为标题

例如:

在window.xaml.cs中:

public List<MyItemType> MyItems { get; set; }

public Window1()
{
    InitializeComponent();

    MyItems = new List<MyItemType>();
    MyItemType newItem = new MyItemType();
    newItem.Image = ... load BMP here ...;
    newItem.Title = "FooBar Icon";
    MyItems.Add(newItem);

    this.MainGrid.DataContext = this;
}
public List MyItems{get;set;}
公共窗口1()
{
初始化组件();
MyItems=新列表();
MyItemType newItem=新的MyItemType();
Image=…在此处加载BMP。。。;
newItem.Title=“FooBar图标”;
MyItems.Add(newItem);
this.MainGrid.DataContext=this;
}
加载图标时,请参阅,因为有很多方法可以执行此操作

然后在window.xaml中:

<Window x:Class="MyApplication.Window1"
    xmlns:local="clr-namespace:MyApplication"
>

<Window.Resources>
    <DataTemplate DataType="{x:Type local:MyItemType}">
       <StackPanel>
           <Image Source="{Binding Path=Icon}"/>
           <TextBlock Text="{Binding Path=Title}"/>
       </StackPanel>
    </DataTemplate>
</Window.Resources>

<Grid Name="MainGrid">
    <ListBox ItemsSource="{Binding Path=MyItems}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</Grid>


我进一步充实了我的示例,这有帮助吗?我曾尝试将此示例绑定到我的代码,但存在一个问题“local:MyItemType未找到”。另外,您能描述一下MyItems.Add是如何工作的吗?local:名称空间只需要在元素:see edit to answer中声明——只要您键入“xmlns:local=”就可以弹出intellisense,您可以从中选择应用程序的名称空间。在本例中,MyItems是List的一个实例,而.Add()方法只是向其中添加一个新项。由于加载图像的方法很多,因此我链接到Microsoft的Imaging Overview,为您提供一个想法。@Eliazar-您必须将其替换为要将列表框绑定到的项目类型。现在没有错误,但列表框不会出现。我已将此转换器用于bmp MemoryStream mstream=new MemoryStream();位图bitmap1=新位图(@“C:\Users\Eliazar\Pictures\1556.bmp”);bitmap1.Save(mstream,System.Drawing.Imaging.ImageFormat.Bmp);字节[]字节1=mstream.ToArray();BinaryWriter=新的BinaryWriter(mstream);writer.Write(字节1);
<Window x:Class="MyApplication.Window1"
    xmlns:local="clr-namespace:MyApplication"
>

<Window.Resources>
    <DataTemplate DataType="{x:Type local:MyItemType}">
       <StackPanel>
           <Image Source="{Binding Path=Icon}"/>
           <TextBlock Text="{Binding Path=Title}"/>
       </StackPanel>
    </DataTemplate>
</Window.Resources>

<Grid Name="MainGrid">
    <ListBox ItemsSource="{Binding Path=MyItems}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</Grid>