我正在製作一個使用Microsoft.Win32.OpenFileDialog的窗體dlg = new Microsoft.Win32.OpenFileDialog();提供文件選擇菜單。將MainWindow.xaml中的TextBox名稱參數傳遞給button_click函數
我想使用相同的功能來更新輸入文件的文本框和輸出文件的文本框。
<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>
所以我希望能夠發送「InputFileBox」或「OutputFileBox」與「BrowseClick」,讓我不必有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
}
感謝
您可以創建一個用戶控件,並在這兩個地方重用。 –
文件名應該是文本框的屬性而不是私有成員。您應該創建一個屬性並將其綁定到文本框 – Gilad
也請在使用WPF之前查看MVVM,不要使用代碼。 – Gilad