我有一个WPF应用程序(窗口1),其中包含一个ScrollViewer。我有第二个窗口,它是同一个WPF应用程序的一部分,并带有一个按钮。
当我在第二个窗口上单击按钮时,我希望它向第一个窗口的ScrollViewer添加一个项目。
我该如何实现这一点?如果我的问题表述不清楚,请见谅。
当我在第二个窗口上单击按钮时,我希望它向第一个窗口的ScrollViewer添加一个项目。
我该如何实现这一点?如果我的问题表述不清楚,请见谅。
正确的做法是让您的第二个窗体具有一个自定义事件,将信息传递给您的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; }
}
}
}
首先,你需要一个DispatcherTimer
,并且需要使用StackPanel
而非Grid
。你还需要在Window1
上声明一个public static bool
。然后,当Window2
上的按钮被点击时,将bool
设置为True
,这意味着要执行一个void函数,将对象添加到StackPanel
或Scroll Viewer
中。
Window1.xaml
首先,您需要将Grid
更改为StackPanel
。假设您有一个名称为scrollViewer1
且Margin = "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
的StackPanel
而不是Grid
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;
}
}
}
谢谢,
希望你觉得这个有帮助 :)