c# - 如何从选定的 WPF ComboBox 项 C# 中显示内容
问题描述
我希望每个人都很好我正在用 C# 和 WPF 构建一个小应用程序,我正在尝试添加一个功能,当单击组合框并选择某个元素时,我想显示文本的内容,但我不能似乎做对了。我想就如何解决这个问题提供一些意见,请查看 C# 代码,如果您有解决方案,请发布,谢谢,请注意您可能从代码中看到,我是初学者编码器。提前感谢您的帮助。
namespace WPFDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
textToDisplay.IsReadOnly = true;
SaveFileDialog saveFileDialog = new SaveFileDialog();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Import new template";
openFileDialog.Filter = "Text files (*.txt)|*.txt";
saveFileDialog.Filter = "Text files (*.txt)|*.txt";
loadTemplates();
}
// Load the templates in the directory
private void loadTemplates()
{
string[] templates = Directory.GetFiles(@"C:\Users\injanlee\source\repos\WPFDemo\WPFDemo\Snow Templates");
Array.Sort(Directory.GetFiles(@"C:\Users\injanlee\source\repos\WPFDemo\WPFDemo\Snow Templates"));
Array.Sort(templates);
foreach (string template in templates)
{
//string fileName = template.Split("\\")[5].Split(".")[0];
string fName = System.IO.Path.GetFileName(template);
selectOption.Items.Add(fName);
}
}
private async Task Sleep(int miliSeconds)
{
await Task.Delay(miliSeconds);
myLabel.Content = " ";
}
private async void Submit_Click(object sender, RoutedEventArgs e)
{
Clipboard.SetText(textToDisplay.Text);
myLabel.Content = "Copied to clipboard";
myLabel.Foreground = new SolidColorBrush(Colors.DarkGreen);
await Sleep(3000);
}
private void DisplayFileContent(string path)
{
textToDisplay.Clear();
try
{
string[] fileContent = File.ReadAllLines(path);
foreach (string line in fileContent)
{
textToDisplay.Text += line;
textToDisplay.Text += "\n";
}
}
catch
{
MessageBox.Show($"Unable to find the path for the specific selected File: {selectOption.Text}");
}
}
// Import and save new file template
public void importTemplate_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
OpenFileDialog openFileDialog = new OpenFileDialog();
saveFileDialog.Title = "Save To";
openFileDialog.Title = "Import new template";
openFileDialog.Filter = "Text files (*.txt)|*.txt";
saveFileDialog.Filter = "Text files (*.txt)|*.txt";
if (openFileDialog.ShowDialog() == true)
{
string[] fileName = openFileDialog.FileName.Split("\\");
string importedFileName = fileName[5].Split(".")[0];
saveFileDialog.FileName = importedFileName;
if (saveFileDialog.ShowDialog() == true)
{
string location = saveFileDialog.FileName;
File.Copy(openFileDialog.FileName, location, true);
}
selectOption.Items.Add(fileName[5].Split(".")[0]);
MessageBox.Show("Impor successfull");
}
}
private void selectOption_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (selectOption.SelectedIndex == 0)
{
DisplayFileContent(@"C:\Users\injanlee\source\repos\WPFDemo\WPFDemo\Snow Templates\Template.txt");
}
}
}
}
解决方案
@aepot 回答了这个问题:
简而言之,您可以使用
selectOption.SelectedItem
属性来获取所选项目的内容。
推荐阅读
- authentication - React,Axios 问题:预检响应没有 HTTP ok 状态(未找到 404)
- typescript - 使用泛型将字符串转换为枚举
- equality - Isabelle 中的术语平等
- android - 带有 Kotlin 1.3.0-rc-57 和 constraintlayout 2.0 的大问题 Android Studio 3.3 Canary 11 (gradle 3.3.0 alpha 11)
- javascript - React - 在组件函数内调用道具函数
- python - 如何将pyaudio输出写入音频文件?
- php - Laravel - Post request empty fields
- python - 我们可以用未标记的数据测试朴素贝叶斯算法吗?
- react-native - 找不到 stacknaigator
- python-3.x - 在路径行中出现错误,. 请指导