简体   繁体   English

WPF 我怎样才能用 1 个事件处理程序做到这一点

[英]WPF How can i do it with 1 event handler

I have a problem, i have 5 buttons that load txt files from system and show it as string on textblocks but i dont know how to do it without 5 event handlers我有一个问题,我有 5 个按钮可以从系统加载 txt 文件并将其显示为文本块上的字符串,但我不知道如何在没有 5 个事件处理程序的情况下执行此操作


    private void OnClick1(object sender, RoutedEventArgs e)
            {            
                OpenFileDialog openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == true)
                numbers1.Text = File.ReadAllText(openFileDialog.FileName);
            }

OnClick1 is button1, numbers1 is a textblock1 now i have 5 codes like this (with numbers2.Text, numbers3.Text etc) how can i do it shorter OnClick1 是 button1, numbers1 是 textblock1 现在我有 5 个这样的代码(带有 numbers2.Text、numbers3.Text 等)我怎么能做得更短

This should get you started I think这应该让你开始我认为

private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = true;
            List<TextBlock> textBlocks = new List<TextBlock>();
            textBlocks.Add(txt1);
            textBlocks.Add(txt2);
            textBlocks.Add(txt3);
            textBlocks.Add(txt4);
            textBlocks.Add(txt5);

            int count = 0;
          
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                
                foreach (String files in openFileDialog.FileNames)
                {
                    var currentText = textBlocks[count];
                    currentText.Text = File.ReadAllText(files);
                    count++;
                }
                
               
            }
        }

Here is the xaml code这是xaml代码

<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="5">
            <Button Name="btnLoad" Content="Load All" Click="btnLoad_Click" ></Button>
            <TextBlock x:Name="txt1" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
            <TextBlock x:Name="txt2" HorizontalAlignment="Left" Margin="5"  TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
            <TextBlock x:Name="txt3" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
            <TextBlock x:Name="txt4" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
            <TextBlock x:Name="txt5" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
        </StackPanel>

I may be misunderstanding what you are asking.我可能误解了你在问什么。 However, if you want the different buttons to use the same click event.但是,如果您希望不同的按钮使用相同的点击事件。 Then the click event is going to have to be able to distinguish “which” button was clicked in order to know which text box to use.然后点击事件必须能够区分“哪个”按钮被点击,以便知道使用哪个文本框。

In this case, I recommend you give each button a name and then in the click event, cast the sender as a Button , then check its name to determine which text box to use.在这种情况下,我建议您为每个按钮命名,然后在单击事件中,将发送者转换为Button ,然后检查其名称以确定要使用的文本框。 All buttons are wired up to this ONE (1) event.所有按钮都连接到这个一 (1) 事件。 Example something like…例如像…

private void btn_Click(object sender, RoutedEventArgs e) {
  Button btnSender = (Button)sender;
  TextBox tb = null;
  switch (btnSender.Name) {
    case "btn1":
      tb = txt1;
      break;
    case "btn2":
      tb = txt2;
      break;
    case "btn3":
      tb = txt3;
      break;
    case "btn4":
      tb = txt4;
      break;
  }
  if (tb != null) {
    OpenFileDialog openFileDialog = new OpenFileDialog();
    if (openFileDialog.ShowDialog() == true)
      tb.Text = File.ReadAllText(openFileDialog.FileName);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM