Click here to Skip to main content
15,936,395 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My load button is in the WPF main window and my listbox is in the user control so now what I want to do is that the name of the file that I load with the load button should come inside the listbox in the user control.

The problem with this code is that when the user control is opened by clicking the UserControl object_placement button and then loading the file by clicking the Load button, the file name is not added to the listbox.

And if the object is loaded with the Direct Load button, the name is added to the listbox

Both the Load button and the Object_Placement button are in the WPF main window

What I have tried:

C#
private void Object_placement_Click(object sender, RoutedEventArgs e)
{
   settingpanel.Children.Clear();
   UserControl1 newFormControl = new UserControl1();
   settingpanel.Children.Add(newFormControl);
}


C#
private void loadbtn_Click(object sender, RoutedEventArgs e)
{
   Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
   dlg.DefaultExt = ".swl";
   dlg.Filter = "SWL files (*.swl)|*.swl";
   Nullable<bool> result = dlg.ShowDialog();
   if (result == true)
   {
      string swlFilePath = dlg.FileName;
      string swlFileName = Path.GetFileNameWithoutExtension(swlFilePath);
      string swlZipFilePath = Path.Combine(Path.GetTempPath(), swlFileName + ".zip");

      if (File.Exists(swlZipFilePath))
      {
         File.Delete(swlZipFilePath);
      }

      // Copy the selected file to the new .zip file
      File.Copy(dlg.FileName, swlZipFilePath);

      // Extract the .zip file to a temporary directory
      string extractDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
      ZipFile.ExtractToDirectory(swlZipFilePath, extractDirectory);
      string Stlfilpath = Path.Combine(extractDirectory, swlFileName+".stl");


       // Add the object name to the existing list
       string stlFileName = Path.GetFileNameWithoutExtension(Stlfilpath);
       myUserControl.objectListBox.Items.Add(stlFileName);

	 }
}
Posted
Updated 13-Apr-23 7:31am
v3
Comments
Richard MacCutchan 13-Apr-23 7:22am    
And? What happens when you run the code?
Ridhdhi Vaghani 13-Apr-23 7:26am    
If the user control is opened by clicking the Object_Placement button and then loading the file by clicking the Load button, the file name is not added to the listbox. And if the object is loaded with the Direct Load button, the name is added to the listbox

1 solution

Here is the simplest example that I can create....

1. Create new WPF project
2. Add UserControl1 UserControl
3. USe the following code:

XAML
XML
<UserControl x:Class="WpfUserControlBinding.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfUserControlBinding"
             mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800">
    <ListBox ItemsSource="{Binding FileNames}" />
</UserControl>

Code-Behind
C#
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        DataContext = this;
    }

    public ObservableCollection<string> FileNames { get; set; } = new();
}

4. Update the MainWindow with the following:

XAML
XML
<Window x:Class="WpfUserControlBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfUserControlBinding"
        mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">
    <local:UserControl1 x:Name="MyUserControl"/>
</Window>

Code-Behind
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        foreach (string entry in Directory
            .EnumerateFileSystemEntries(Environment.CurrentDirectory))
            MyUserControl.FileNames.Add(Path.GetFileName(entry));
    }
}


Run the app and the ListBox will be populated with filenames.
 
Share this answer
 
Comments
Ridhdhi Vaghani 13-Apr-23 8:02am    
I get error in this line :- public ObservableCollection<string> FileNames { get; set; } = new(); (ERROR :- feature 'target-type object creation' is not available in C# 7.3, please use language version 9.0 or greater.) (i cannot change language version)
Graeme_Grant 13-Apr-23 8:26am    
I'm using modern syntax. Very easy to change.
public ObservableCollection<string> FileNames { get; set; }
    = new ObservableCollection<string>();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900