【解決方法】リストボックスにファイル名を保存するには?


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

私が試したこと:

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);

	 }
}

解決策 1

これが私が作成できる最も簡単な例です….

1. 新しい WPF プロジェクトを作成する
2.追加 UserControl1 ユーザーコントロール
3. 次のコードを使用します。

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>

コードビハインド

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

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

4. MainWindow を次のように更新します。

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>

コードビハインド

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

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

アプリを実行し、 ListBox ファイル名が入力されます。

コメント

タイトルとURLをコピーしました