简体   繁体   English

如何在 WindowsXamlHost ListView 中添加项目?

[英]How do I add an item in a WindowsXamlHost ListView?

I tried to add an item to a WindowsXamlHost whose InitialTypeName is set to Windows.UI.Xaml.Controls.ListView .我尝试向WindowsXamlHost添加一个项目,其InitialTypeName设置为Windows.UI.Xaml.Controls.ListView I am trying to add an item to the WindowsXamlHost listview.我正在尝试向 WindowsXamlHost 列表视图添加一个项目。

Here is my code:这是我的代码:

List<string> listFiles = new List<string>();

OpenFileDialog openFileDialog = new OpenFileDialog() { DereferenceLinks = false };

        private void AddItem_Click(object sender, RoutedEventArgs e)
        {



            if (openFileDialog.ShowDialog() == true)
            {
                foreach(String myfile in openFileDialog.FileNames)
                {

                    //get filename
                    string filename = System.IO.Path.GetFileName(myfile);

                    if (Path.GetExtension(myfile) == ".lnk")
                    {
                        try
                        {
                            var iconmyfile = GetShortcutTargetFile(myfile);
                            Icon icon1 = System.Drawing.Icon.ExtractAssociatedIcon(iconmyfile);
                            Bitmap icon = icon1.ToBitmap();

                            StackPanel sp = new StackPanel();



                            listView.Items.Add(sp);

                            sp.Height = 35;
                            sp.Width = listView.Width;
                            sp.Orientation = Orientation.Horizontal;
                            sp.VerticalAlignment = VerticalAlignment.Center;

                            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
                            image.Source = BitmapToImageSource(icon);
                            sp.Children.Add(image);

                            TextBlock label = new TextBlock();
                            label.VerticalAlignment = VerticalAlignment.Center;


                            label.Text = "  " + filename.Remove(filename.Length - 4);
                            label.FontSize = 17;

                            sp.Children.Add(label);
                        }
                        catch
                        {
                            MessageBox.Show("It seems that the shortcut file that you have chosen has no target location. Please select another file.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                    else
                    {

                        Icon icon1 = System.Drawing.Icon.ExtractAssociatedIcon(myfile);
                        Bitmap icon = icon1.ToBitmap();

                        StackPanel sp = new StackPanel();

                        listView.Items.Add(sp);

                        sp.Height = 35;
                        sp.Width = listView.Width;
                        sp.Orientation = Orientation.Horizontal;
                        sp.VerticalAlignment = VerticalAlignment.Center;

                        System.Windows.Controls.Image image = new System.Windows.Controls.Image();
                        image.Source = BitmapToImageSource(icon);
                        sp.Children.Add(image);

                        TextBlock label = new TextBlock();
                        label.VerticalAlignment = VerticalAlignment.Center;


                        label.Text = "  " + filename.Remove(filename.Length - 4);
                        label.FontSize = 17;

                        sp.Children.Add(label);
                    }

                    //TODO: write text documents to directory and get the file name





                }
            }
        }

This code works fine with a regular WPF ListView.此代码适用于常规 WPF ListView。 However, when I try to use this code with the WindowsXamlHost ListView from XAML Islands, it says that "WindowsXamlHost does not contain a definition for 'Items'."但是,当我尝试将此代码与来自 XAML 群岛的 WindowsXamlHost ListView 一起使用时,它说“WindowsXamlHost 不包含 'Items' 的定义。”

Can anyone help me?谁能帮我?

Thanks谢谢

You should cast the Child property of the WindowsXamlHost to a Windows.UI.Xaml.Controls.ListView (or whatever the InitialTypeName is set to) before you try to access its Items property:在尝试访问其Items属性之前,您应该将WindowsXamlHostChild属性转换为Windows.UI.Xaml.Controls.ListView (或InitialTypeName设置的任何InitialTypeName ):

var uwpListView = listView.Child as Windows.UI.Xaml.Controls.ListView;
uwpListView.Items.Add(...);

You should probably also rename the WindowsXamlHost to something better than "listView".您可能还应该将WindowsXamlHost重命名为比“listView”更好的名称。 After all, it's just a host for the UWP control.毕竟它只是一个 UWP 控件的宿主

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将项目添加到ListView? - How do I add an item to a ListView? 如何将具有自定义项的自定义项目添加到ListView - How do I add a custom item to a ListView with a Collection binding 如何添加到 Listview 项目中的特定列? - How do I add to a specific column in Listview item? 当我从列表视图中选择项目并在WPF上的UI中单击添加按钮时,如何将项目添加到列表中 - How do I add item to a list when I select the item from a listview and click add button in the UI on WPF 我如何在C#WPF中在ListView中添加ContextMenu(想在某人确实单击ListView项目时添加“修改”操作) - How i can add ContextMenu in ListView (want to add “Modify” operation when someone do right click on ListView item ) in C# WPF 如何在不引起异常的情况下从另一个线程向ListView添加项目 - How do I add an item to a ListView from another thread without causing an exception 如何获取图像ListView中所选项目的索引? - How do I get the index of a selected item in an image ListView? 如何更新绑定到WPF中集合的listview项? - How do I update a listview item that is bound to a collection in WPF? 我如何使用ListView.Item.CurrentItem - how do i work with ListView.Item.CurrentItem 如何在列表视图中显示项目的文本? - How do I display the text of an item in its listview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM