WPF – 如何将带有图像的ListView添加到Datagrid-RowDetail

huangapple go评论77阅读模式
英文:

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(&quot;users&quot;);
QuerySnapshot snap = await docref.GetSnapshotAsync();

foreach (DocumentSnapshot docsnap in snap){

    Users Employee = docsnap.ConvertTo&lt;Users&gt;();

    if (docsnap.Exists) {

        List&lt;string&gt; AuthorList = new List&lt;string&gt;();
        string UrlLinks = &quot;&quot;;
        for (int i = 0; i &lt; 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&lt;string&gt; imagepath { get; set; }   
    }
}

and my xaml file for the GUI

&lt;DataGrid.RowDetailsTemplate&gt;

                &lt;DataTemplate&gt;
                    
                    &lt;DockPanel Background=&quot;GhostWhite&quot;&gt;
                        &lt;StackPanel Orientation=&quot;Horizontal&quot; &gt;
                            
                            &lt;ListView Name=&quot;listview1&quot; ItemsSource=&quot;{Binding imagepath}&quot;&gt;
                                &lt;ListView.ItemTemplate&gt;
                                    &lt;DataTemplate&gt;
                                        &lt;StackPanel&gt;
                                            &lt;Image Source=&quot;{Binding imagepath}&quot; /&gt;
                                        &lt;/StackPanel&gt;
                                    &lt;/DataTemplate&gt;
                                &lt;/ListView.ItemTemplate&gt;
                            &lt;/ListView&gt;
                            
                      
                        &lt;/StackPanel&gt;
                    
                        &lt;Grid Margin=&quot;0,10&quot;&gt;
                        &lt;Grid.ColumnDefinitions&gt;
                            &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
                            &lt;ColumnDefinition Width=&quot;*&quot; /&gt;
                        &lt;/Grid.ColumnDefinitions&gt;
                            &lt;Grid.RowDefinitions&gt;
                                &lt;RowDefinition Height=&quot;Auto&quot; /&gt;
                                &lt;RowDefinition Height=&quot;Auto&quot; /&gt;
                                &lt;RowDefinition Height=&quot;Auto&quot; /&gt;
                            &lt;/Grid.RowDefinitions&gt;


                            &lt;TextBlock  x:Name=&quot;Test123&quot;  Text=&quot;ID: &quot; FontWeight=&quot;Bold&quot; /&gt;
                        &lt;TextBlock Text=&quot;{Binding id}&quot; Grid.Column=&quot;1&quot; /&gt;
                        &lt;TextBlock Text=&quot;Name: &quot; FontWeight=&quot;Bold&quot; Grid.Row=&quot;1&quot; /&gt;
                        &lt;TextBlock Text=&quot;{Binding name}&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;1&quot; /&gt;
                        &lt;TextBlock Text=&quot;Birthday: &quot; FontWeight=&quot;Bold&quot; Grid.Row=&quot;2&quot; /&gt;
                        &lt;TextBlock Text=&quot;{Binding birthday, StringFormat=d}&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;2&quot; /&gt;
                           
                        &lt;/Grid&gt;
                &lt;/DockPanel&gt;
            &lt;/DataTemplate&gt;
        &lt;/DataGrid.RowDetailsTemplate&gt;

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:

&lt;ListView Name=&quot;tt&quot; ItemsSource=&quot;{Binding imagepath}&quot;&gt;
    &lt;ListView.ItemTemplate&gt;
        &lt;DataTemplate&gt;
            &lt;StackPanel&gt;
                &lt;Image Source=&quot;{Binding}&quot; /&gt;
            &lt;/StackPanel&gt;
        &lt;/DataTemplate&gt;
    &lt;/ListView.ItemTemplate&gt;
&lt;/ListView&gt;

As stated in a comment by @ASh, you should also create one List&lt;string&gt; per Users object in your code:

async void getAllData()
{

    Query docref = database.Collection(&quot;users&quot;);
    QuerySnapshot snap = await docref.GetSnapshotAsync();
    foreach (DocumentSnapshot docsnap in snap)
    {
        Users Employee = docsnap.ConvertTo&lt;Users&gt;();
        List&lt;string&gt; AuthorList = new List&lt;string&gt;();
        Employee.imagepath = AuthorList;

        if (docsnap.Exists)
        {

            string UrlLinks = &quot;&quot;;
            for (int i = 0; i &lt; 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);
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月8日 20:45:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定