WPF -如何将带有图像的ListView添加到数据网格中-RowDetail

ogsagwnx  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(173)

我想在我的DataGrid-Rowdetails中添加一个Listview,其中包含从我的firebase Cloud加载的imageURL。
在我的方法中,我从firebase接收所有数据并将其转换为用户对象。URL数组get被追加到列表中并转换为用户对象。
方法如下:

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);
                }
            }
        }

我的用户类:

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 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>

我做错了什么,或者一般来说是不可能的?
可能重复

我尝试创建一个列表并将该列表附加到用户对象

ttcibm8c

ttcibm8c1#

如果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);
            }
            // Every URL links get printed out MessageBox.Show(UrlLinks);
            DataGridXAML.Items.Add(Employee);
        }
    }
}

相关问题