簡體   English   中英

如何顯示所選 WPF ComboBox 項目 C# 中的內容

[英]How to display content from selected WPF ComboBox item 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屬性來獲取所選項目的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM