【问题标题】:How do I add an item in a WindowsXamlHost ListView?如何在 WindowsXamlHost ListView 中添加项目?
【发布时间】:2020-04-01 08:38:32
【问题描述】:

我尝试将一个项目添加到 WindowsXamlHostInitialTypeName 设置为 Windows.UI.Xaml.Controls.ListView。我正在尝试向 WindowsXamlHost 列表视图添加一个项目。

这是我的代码:

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





                }
            }
        }

此代码适用于常规 WPF ListView。但是,当我尝试将此代码与 XAML Islands 中的 WindowsXamlHost ListView 一起使用时,它会显示“WindowsXamlHost 不包含‘Items’的定义。”

谁能帮帮我?

谢谢

【问题讨论】:

    标签: c# wpf listview windows-community-toolkit xaml-islands


    【解决方案1】:

    在尝试访问其Items 属性之前,您应该将WindowsXamlHostChild 属性转换为Windows.UI.Xaml.Controls.ListView(或任何InitialTypeName 设置):

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

    您可能还应该将WindowsXamlHost 重命名为比“listView”更好的名称。毕竟,它只是 UWP 控件的宿主

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      相关资源
      最近更新 更多