WPF - 在 Wrappannel 中显示图像和文本框
WPF - Display Image in Wrappannel along with textbox
嗨,Whosebug 专家,
最近我才开始用 WPF 编码并卡在某个地方。
我必须显示图像列表和文本,我试图在 google 上找到,但像往常一样所有都使用硬编码值。
第 1 步:在 XAML 文件中,我使用了环绕面板,代码如下:
<ListView Width="auto" Name="listviewSource" BorderBrush="{x:Null}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="auto" Height="150" Orientation="Vertical" UseLayoutRounding="True" / >
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
第二步:目前我可以成功绑定所有图片,代码:
Image imageData = new Image();
List<Image> ImageList = new List<Image>(); // List of Image
List<string> imageName = new List<string>(); // list of name
imageData = bitmapData // here bitmapData is having data which is already computed
imageName.Add(name); // This is the issue, how to display name below image
ImageList.Add(imageData );
listviewSource.ItemSource = ImageList;
您应该在要放置名称的 XAML 中有一个 Label
或 TextBlock
,并且您应该将 imageName
设置为它。
XAML 中的类似内容:
<ListView Width="auto" Name="listviewSource" BorderBrush="{x:Null}" Margin="10,10,10,10">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="auto" Height="150" Orientation="Vertical" UseLayoutRounding="True" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<Image Name="DisplayedImage" Height="100" Width="100"/>
<Label Name="ImageName" Content="ImagesName" Height="32" Width="93"/>
</ListView>
CS 文件:
Image imageData = new Image();
List<Image> ImageList = new List<Image>(); // List of Images
List<string> imageName = new List<string>(); // list of names
string name= "...";
BitmapImage myBitmapImage = new BitmapImage();
//Setting BitmapImage properties must be done within a BeginInit and EndInit block.
myBitmapImage.BeginInit();
myBitmapImage.UriSource= new Uri(@"C:\Users\...\Whosebug.bmp");
myBitmapImage.DecodePixelWidth = 100;
myBitmapImage.EndInit();
imageData.Source= myBitmapImage; // here bitmapData is having data which is already computed
imageName.Add(name);
ImageName.Content= name;// This is the issue, how to display name below image
DisplayedImage.Source = myBitmapImage;
ImageList.Add(imageData);
您的 ListView
从 ImageList 获取 ItemSource
。 imageName
未在任何地方使用。你想要的是也将它绑定到你的 ListView
。我的建议是创建一个 class MyImageClass
,其中包含一个 Property
类型的图像和另一个 Property
类型的 string
并连接您的 listviewSource
至 list<MyImageClass>
。在您的用户控件中,在 WrapPanel
内,添加 <Image/>
和 <Label/>
并将它们绑定到成员 myImage
和 myImageName
.
嗨,Whosebug 专家, 最近我才开始用 WPF 编码并卡在某个地方。 我必须显示图像列表和文本,我试图在 google 上找到,但像往常一样所有都使用硬编码值。
第 1 步:在 XAML 文件中,我使用了环绕面板,代码如下:
<ListView Width="auto" Name="listviewSource" BorderBrush="{x:Null}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="auto" Height="150" Orientation="Vertical" UseLayoutRounding="True" / >
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
第二步:目前我可以成功绑定所有图片,代码:
Image imageData = new Image();
List<Image> ImageList = new List<Image>(); // List of Image
List<string> imageName = new List<string>(); // list of name
imageData = bitmapData // here bitmapData is having data which is already computed
imageName.Add(name); // This is the issue, how to display name below image
ImageList.Add(imageData );
listviewSource.ItemSource = ImageList;
您应该在要放置名称的 XAML 中有一个 Label
或 TextBlock
,并且您应该将 imageName
设置为它。
XAML 中的类似内容:
<ListView Width="auto" Name="listviewSource" BorderBrush="{x:Null}" Margin="10,10,10,10">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="auto" Height="150" Orientation="Vertical" UseLayoutRounding="True" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<Image Name="DisplayedImage" Height="100" Width="100"/>
<Label Name="ImageName" Content="ImagesName" Height="32" Width="93"/>
</ListView>
CS 文件:
Image imageData = new Image();
List<Image> ImageList = new List<Image>(); // List of Images
List<string> imageName = new List<string>(); // list of names
string name= "...";
BitmapImage myBitmapImage = new BitmapImage();
//Setting BitmapImage properties must be done within a BeginInit and EndInit block.
myBitmapImage.BeginInit();
myBitmapImage.UriSource= new Uri(@"C:\Users\...\Whosebug.bmp");
myBitmapImage.DecodePixelWidth = 100;
myBitmapImage.EndInit();
imageData.Source= myBitmapImage; // here bitmapData is having data which is already computed
imageName.Add(name);
ImageName.Content= name;// This is the issue, how to display name below image
DisplayedImage.Source = myBitmapImage;
ImageList.Add(imageData);
您的 ListView
从 ImageList 获取 ItemSource
。 imageName
未在任何地方使用。你想要的是也将它绑定到你的 ListView
。我的建议是创建一个 class MyImageClass
,其中包含一个 Property
类型的图像和另一个 Property
类型的 string
并连接您的 listviewSource
至 list<MyImageClass>
。在您的用户控件中,在 WrapPanel
内,添加 <Image/>
和 <Label/>
并将它们绑定到成员 myImage
和 myImageName
.