Home>
Label, textbox, and button are 3 rows, 5 rows in a row, and when the button is pressed, Explorer is opened and the selected file name is displayed in textbox.
I would like to do that, but I asked the question this time because I didn't know how to associate the button and textbox as shown in the example below.
I checked with the tag property, binding, etc., but it did not come up with a solution to see if this was not the case.

Example)
When the button on the second line is pressed, the file name is displayed in the textbox on the second line.
When the button on the third line is pressed, the file name is displayed in the textbox on the third line.

I ’m just starting to study and I ’m very sorry for the introductory question, but I ’d appreciate it.

Applicable source code
<Window x: Class = "VoiceApp.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: App"
        mc: Ignorable = "d"
        Title = "MainWindow" Height = "350" Width = "500">
    <StackPanel>
        <StackPanel Orientation = "Horizontal" HorizontalAlignment = "Center" Height = "30">
            <Label Content = "1" Width = "30" Height = "25" Background = "Transparent"></Label>
            <TextBox Name = "one" Width = "30" Height = "25" Background = "Transparent"></TextBox>
            <Button Content = "Reference" Width = "40" Height = "20" Click = "reference_Click"></Button>
        </StackPanel>
        <StackPanel Orientation = "Horizontal" HorizontalAlignment = "Center" Height = "30">
            <Label Content = "2" Width = "30" Height = "25" Background = "Transparent"></Label>
            <TextBox Name = "two" Width = "30" Height = "25" Background = "Transparent"></TextBox>
            <Button Content = "Reference" Width = "40" Height = "20" Click = "reference_Click"></Button>
        </StackPanel>
        <StackPanel Orientation = "Horizontal" HorizontalAlignment = "Center" Height = "30"><Label Content = "3" Width = "30" Height = "25" Background = "Transparent"></Label>
            <TextBox Name = "tree" Width = "30" Height = "25" Background = "Transparent"></TextBox>
            <Button Content = "Reference" Width = "40" Height = "20" Click = "reference_Click"></Button>
        </StackPanel>
        <StackPanel Orientation = "Horizontal" HorizontalAlignment = "Center" Height = "30">
            <Label Content = "4" Width = "30" Height = "25" Background = "Transparent"></Label>
            <TextBox Name = "four" Width = "30" Height = "25" Background = "Transparent"></TextBox>
            <Button Content = "Reference" Width = "40" Height = "20" Click = "reference_Click"></Button>
        </StackPanel>
        <StackPanel Orientation = "Horizontal" HorizontalAlignment = "Center" Height = "30">
            <Label Content = "5" Width = "30" Height = "25" Background = "Transparent"></Label>
            <TextBox Name = "five" Width = "30" Height = "25" Background = "Transparent"></TextBox>
            <Button Content = "Reference" Width = "40" Height = "20" Click = "reference_Click"></Button>
        </StackPanel>
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;using System.Windows.Shapes;
namespace App
{
    ///<summary>
    /// MainWindow.xaml interaction logic
    ///</summary>
    public partial class MainWindow: Window
    {
        public MainWindow ()
        {
            InitializeComponent ();
        }
        private void reference_Click (object sender, RoutedEventArgs e)
        {
            // Instantiate dialog
            var dialog = new OpenFileDialog ();
            // Set the file type of the dialog
            dialog.Filter = "Voice file | * .wav";
            // Display dialog
            dialog.ShowDialog ();
            // describe the process of storing the file name in the text box
        }
    }
}

Please describe what you tried for the problem here.

Supplemental information (FW/tool version etc.)

Please provide more information here.

  • Answer # 1

    This method uses the abandoned tag property and binding.

    
        
        
        
    


    The code is

    instead ofdialog.ShowDialog ();
    if (dialog.ShowDialog () == true)
        {
            if (sender is Button b)
            {
                b.Tag = dialog.SafeFileName;
            }
        }