[英]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
属性之前,您应该将WindowsXamlHost
的Child
属性转换为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.