I tried to add an item to a WindowsXamlHost
whose InitialTypeName
is set to Windows.UI.Xaml.Controls.ListView
. I am trying to add an item to the WindowsXamlHost listview.
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. 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'."
Can anyone help me?
Thanks