英文:
WPF - how to add a ListView with images into Datagrid-RowDetail
问题
我想在我的DataGrid-Rowdetails中添加一个ListView,其中包含来自我的Firebase Cloud的加载图像URLs。
在我的方法中,我从Firebase接收所有数据并将其转换为用户对象。URL数组被附加到一个列表中,并转换为用户对象。
这是方法:
async void getAllData() {
Query docref = database.Collection("users");
QuerySnapshot snap = await docref.GetSnapshotAsync();
foreach (DocumentSnapshot docsnap in snap) {
Users Employee = docsnap.ConvertTo<Users>();
if (docsnap.Exists) {
List<string> AuthorList = new List<string>();
string UrlLinks = "";
for (int i = 0; i < Employee.ImageUrl.Length; i++) {
string URLS = Employee.ImageUrl[i].ToString();
UrlLinks += URLS + Environment.NewLine;
AuthorList.Add(URLS);
Employee.imagepath = AuthorList;
}
// 每个URL链接都会被打印出来 MessageBox.Show(UrlLinks);
DataGridXAML.Items.Add(Employee);
}
}
}
我的User类:
namespace First_WPF_Project {
[FirestoreData]
public class Users {
[FirestoreProperty]
public string id { get; set; }
[FirestoreProperty]
public int age { get; set; }
[FirestoreProperty]
public string birthday { get; set; }
[FirestoreProperty]
public string name { get; set; }
[FirestoreProperty]
public string[] ImageUrl { get; set; }
public List<string> imagepath { get; set; }
}
}
以及用于GUI的XAML文件:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DockPanel Background="GhostWhite">
<StackPanel Orientation="Horizontal">
<ListView Name="listview1" ItemsSource="{Binding imagepath}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
<Grid Margin="0,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock x:Name="Test123" Text="ID:" FontWeight="Bold" />
<TextBlock Text="{Binding id}" Grid.Column="1" />
<TextBlock Text="Name:" FontWeight="Bold" Grid.Row="1" />
<TextBlock Text="{Binding name}" Grid.Column="1" Grid.Row="1" />
<TextBlock Text="Birthday:" FontWeight="Bold" Grid.Row="2" />
<TextBlock Text="{Binding birthday, StringFormat=d}" Grid.Column="1" Grid.Row="2" />
</Grid>
</DockPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
你的代码看起来基本上是正确的,但你需要确保ListView的ItemsSource
属性绑定到imagepath
,并且在DataTemplate中使用{Binding}
来绑定图像的源。如果你仍然遇到问题,请提供更多详细信息,以便我能更好地帮助你。
英文:
I want to add inside my Datagrid-Rowdetails a Listview with my loaded imageURls from my firebase Cloud.
In my method I recieve all data from firebase and convert it into a user object. The Url-array get's appended into a list and converted to the user object.
Here is the method:
async void getAllData() {
Query docref = database.Collection("users");
QuerySnapshot snap = await docref.GetSnapshotAsync();
foreach (DocumentSnapshot docsnap in snap){
Users Employee = docsnap.ConvertTo<Users>();
if (docsnap.Exists) {
List<string> AuthorList = new List<string>();
string UrlLinks = "";
for (int i = 0; i < Employee.ImageUrl.Length; i++) {
string URLS = Employee.ImageUrl[i].ToString();
UrlLinks += URLS + Environment.NewLine;
AuthorList.Add(URLS);
Employee.imagepath = AuthorList;
}
// Every URL links get printed out MessageBox.Show(UrlLinks);
DataGridXAML.Items.Add(Employee);
}
}
}
My User-Class:
namespace First_WPF_Project
{
[FirestoreData]
public class Users
{
[FirestoreProperty]
public string id { get; set; }
[FirestoreProperty]
public int age { get; set; }
[FirestoreProperty]
public string birthday { get; set; }
[FirestoreProperty]
public string name { get; set; }
[FirestoreProperty]
public string[] ImageUrl { get; set; }
public List<string> imagepath { get; set; }
}
}
and my xaml file for the GUI
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DockPanel Background="GhostWhite">
<StackPanel Orientation="Horizontal" >
<ListView Name="listview1" ItemsSource="{Binding imagepath}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding imagepath}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
<Grid Margin="0,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock x:Name="Test123" Text="ID: " FontWeight="Bold" />
<TextBlock Text="{Binding id}" Grid.Column="1" />
<TextBlock Text="Name: " FontWeight="Bold" Grid.Row="1" />
<TextBlock Text="{Binding name}" Grid.Column="1" Grid.Row="1" />
<TextBlock Text="Birthday: " FontWeight="Bold" Grid.Row="2" />
<TextBlock Text="{Binding birthday, StringFormat=d}" Grid.Column="1" Grid.Row="2" />
</Grid>
</DockPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
What do I do wrong or is it not possible in general?
Possible duplications
I've tried to create a list and append the list to the user object
答案1
得分: 0
如果 imagepath
属性包含有效的图像URI,应该可以工作:
<ListView Name="tt" ItemsSource="{Binding imagepath}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
根据 @ASh 的评论所述,您还应该在代码中为每个 Users
对象创建一个 List<string>
:
async void getAllData()
{
Query docref = database.Collection("users");
QuerySnapshot snap = await docref.GetSnapshotAsync();
foreach (DocumentSnapshot docsnap in snap)
{
Users Employee = docsnap.ConvertTo<Users>();
List<string> AuthorList = new List<string>();
Employee.imagepath = AuthorList;
if (docsnap.Exists)
{
string UrlLinks = "";
for (int i = 0; i < Employee.ImageUrl.Length; i++)
{
string URLS = Employee.ImageUrl[i].ToString();
UrlLinks += URLS + Environment.NewLine;
AuthorList.Add(URLS);
}
// 每个URL链接都会打印出 MessageBox.Show(UrlLinks);
DataGridXAML.Items.Add(Employee);
}
}
}
英文:
If the imagepath
property contains valid image URIs, this should work:
<ListView Name="tt" ItemsSource="{Binding imagepath}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
As stated in a comment by @ASh, you should also create one List<string>
per Users
object in your code:
async void getAllData()
{
Query docref = database.Collection("users");
QuerySnapshot snap = await docref.GetSnapshotAsync();
foreach (DocumentSnapshot docsnap in snap)
{
Users Employee = docsnap.ConvertTo<Users>();
List<string> AuthorList = new List<string>();
Employee.imagepath = AuthorList;
if (docsnap.Exists)
{
string UrlLinks = "";
for (int i = 0; i < Employee.ImageUrl.Length; i++)
{
string URLS = Employee.ImageUrl[i].ToString();
UrlLinks += URLS + Environment.NewLine;
AuthorList.Add(URLS);
}
// Every URL links get printed out MessageBox.Show(UrlLinks);
DataGridXAML.Items.Add(Employee);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论