将文本框名称参数从 MainWindow.xaml 传递到 button_click 函数
Passing a TextBox name parameter from MainWindow.xaml to button_click Function
我正在制作一个使用 Microsoft 的表单。Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();提供文件选择菜单。
我想使用相同的函数更新输入文件的文本框和输出文件的文本框。
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Width="515" Height="96" VerticalAlignment="Top">
<TextBlock Text="Input File:" VerticalAlignment="Center" />
<TextBox x:Name="InputFileBox" Width ="213" VerticalAlignment="Center" TextChanged="InputFileBox_TextChanged" Height="17" Margin="0,39,0,40" />
<Button Content="Browse" Width="47" Margin="0,39,0,40" RenderTransformOrigin="1.599,0.714" Click="Browse_Click"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Width="515" Height="96" VerticalAlignment="Top">
<TextBlock Text="Output File:" VerticalAlignment="Center" />
<TextBox x:Name="OutputFileBox" Width ="213" VerticalAlignment="Center" TextChanged="OutputFileBox_TextChanged" Height="17" Margin="0,39,0,40" />
<Button Content="Browse2" Width="47" Margin="0,39,0,40" RenderTransformOrigin="1.599,0.714" Click="Browse_Click"/>
</StackPanel>
所以我希望能够使用 "BrowseClick" 发送 "InputFileBox" 或 "OutputFileBox",这样我就不必使用 BrowseInputClick 和 BrowseOutputClick 函数。
在 Browse_Click 函数中,我希望能够执行如下操作:
private void Browse_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
// I don't know what to put here: input/outputTextBoxName = filename
}
谢谢
在 WPF 中,您可以设置按钮的标签 属性。
完成后,您可以使用
在点击处理程序中获取标签 属性
在 XAML 中添加 Tag="input"
作为 inputTextBox 的属性之一,并将 Tag="output"
添加到 outputTextBox(例如:<TextBox x:Name="inputTextBox" Tag="input"/>
)
var tag = (sender as Button).Tag;
然后:
if (tag == 'input')
inputTextBox.Text = filename;
else outputTextBox.Text = filename;
我正在制作一个使用 Microsoft 的表单。Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();提供文件选择菜单。
我想使用相同的函数更新输入文件的文本框和输出文件的文本框。
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Width="515" Height="96" VerticalAlignment="Top">
<TextBlock Text="Input File:" VerticalAlignment="Center" />
<TextBox x:Name="InputFileBox" Width ="213" VerticalAlignment="Center" TextChanged="InputFileBox_TextChanged" Height="17" Margin="0,39,0,40" />
<Button Content="Browse" Width="47" Margin="0,39,0,40" RenderTransformOrigin="1.599,0.714" Click="Browse_Click"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Width="515" Height="96" VerticalAlignment="Top">
<TextBlock Text="Output File:" VerticalAlignment="Center" />
<TextBox x:Name="OutputFileBox" Width ="213" VerticalAlignment="Center" TextChanged="OutputFileBox_TextChanged" Height="17" Margin="0,39,0,40" />
<Button Content="Browse2" Width="47" Margin="0,39,0,40" RenderTransformOrigin="1.599,0.714" Click="Browse_Click"/>
</StackPanel>
所以我希望能够使用 "BrowseClick" 发送 "InputFileBox" 或 "OutputFileBox",这样我就不必使用 BrowseInputClick 和 BrowseOutputClick 函数。
在 Browse_Click 函数中,我希望能够执行如下操作:
private void Browse_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
// I don't know what to put here: input/outputTextBoxName = filename
}
谢谢
在 WPF 中,您可以设置按钮的标签 属性。
完成后,您可以使用
在点击处理程序中获取标签 属性在 XAML 中添加 Tag="input"
作为 inputTextBox 的属性之一,并将 Tag="output"
添加到 outputTextBox(例如:<TextBox x:Name="inputTextBox" Tag="input"/>
)
var tag = (sender as Button).Tag;
然后:
if (tag == 'input')
inputTextBox.Text = filename;
else outputTextBox.Text = filename;