【问题标题】:Button click event to affect a different window按钮单击事件以影响不同的窗口
【发布时间】:2012-10-17 07:05:17
【问题描述】:

我有一个带有 ScrollViewer 的 WPF 应用程序(窗口 1)。我有第二个窗口,它是同一个 WPF 应用程序的一部分,上面有一个按钮。

当我单击第二个窗口上的按钮时,我希望它向第一个窗口上的 ScrollViewer 添加一个项目。

我怎样才能做到这一点?抱歉,如果这含糊不清,我不知道如何问这个问题。

【问题讨论】:

    标签: c# wpf events button click


    【解决方案1】:

    首先,您需要DispatcherTimer,并且您必须使用StackPanel 而不是Grid。您还需要在Window1 上声明public static bool。然后,当点击Window2 上的按钮时,bool 被设置为True,这意味着执行一个将对象添加到StackPanelScroll Viewer 的void

    XAML

    Window1.xaml

    首先,您需要将Grid 更改为StackPanel。假设您有一个名为scrollViewer1Margin = "37,36,58,36"ScrollViewer,您应该尝试以下操作

    更改以下内容

    <Grid>
        <ScrollViewer Margin="37,36,58,36" Name="scrollViewer1" />
    </Grid>
    

    <StackPanel x:Name="stackPanel">
        <ScrollViewer Margin="37,36,58,36" Name="scrollViewer1" />
    </StackPanel>
    

    这样您就可以使用StackPanel 而不是名称为stackPanelGrid


    C#

    Window1.xaml

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public static bool AddItem = false; //This must be public and static so that it can be called from your second Window
            public Window1()
            {
                InitializeComponent();
            }
            public void AddToScrollViewer()
            {
                Button _Object = new Button(); //Create a new object, change button to the UIElement you would like to be
                stackPanel.Children.Add(_Object); //Add the UIElement to the StackPanel
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); //Initialize a new timer object
                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); //Link the Tick event with dispatcherTimer_Tick
                dispatcherTimer.Interval = new TimeSpan(0, 0, 1); //Set the Timer Interval
                dispatcherTimer.Start(); //Start the Timer
            }
    
            private void dispatcherTimer_Tick(object sender, EventArgs e)
            {
                if (AddItem)
                {
                    AddItem = false;
                    AddToScrollViewer();
                }
            }
        }
    }
    

    Window2.xaml

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for Window2.xaml
        /// </summary>
        public partial class Window2 : Window
        {
            public Window2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                Window1.AddItem = true;
            }
        }
    }
    

    谢谢,
    希望对您有所帮助:)

    【讨论】:

      【解决方案2】:

      执行此操作的正确方法是让您的第二个表单有一个 custom Event 将信息传递到您的 MainWindow。

      这是一个快速的概念证明,看看它是否适合你。

      MainWindow.xaml.cs
      1 个滚动查看器 1 个滚动查看器中的堆栈面板和 1 个按钮:

      namespace WpfApplication1
      {
          /// <summary>
          /// Interaction logic for MainWindow.xaml
          /// </summary>
          public partial class MainWindow : Window
          {
              Window2 secondWindow;
              public MainWindow()
              {
                  InitializeComponent();
      
              }
      
              private void button1_Click(object sender, RoutedEventArgs e)
              {
                  secondWindow = new Window2();
                  secondWindow.RaiseCustomEvent += new Window2.myCustomEventHandler(secondWindow_RaiseCustomEvent);
                  secondWindow.ShowDialog();
              }
      
              void secondWindow_RaiseCustomEvent(object sender, myCustomEventArgs e)
              {
                  Label lbl= new Label();
                  lbl.Content = string.Copy(e.retrieveString);
                  ((StackPanel)scrollViewer1.Content).Children.Add(lbl);
              }
          }
      }
      

      Window2.xaml.cs
      1 个按钮

      namespace WpfApplication1
      {
          /// <summary>
          /// Interaction logic for Window2.xaml
          /// </summary>
      
         public partial class Window2 : Window
         {
              public delegate void myCustomEventHandler(object sender, myCustomEventArgs e);
              public event myCustomEventHandler RaiseCustomEvent;
              String myStatement = "Hello World";
              public Window2()
              {
                  InitializeComponent();
              }
      
              private void button1_Click(object sender, RoutedEventArgs e)
              {
                  myCustomEventArgs ea = new myCustomEventArgs( myStatement);
                  RaiseCustomEvent(sender, ea);
      
              }
          }
      
          public class myCustomEventArgs : EventArgs
          {
              public myCustomEventArgs(string s)
              {
                  myString = s;
              }
              private string myString;
              public string retrieveString
              {
                  get { return myString; }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多