我有一个带有ScrollViewer的WPF应用程序(窗口1)。我有一个第二个窗口,它是同一个WPF应用程序的一部分,上面有一个按钮。
当我单击第二个窗口上的按钮时,我希望它在第一个窗口中将项目添加到ScrollViewer。
我怎样才能做到这一点?对不起,如果这是模糊的,我不知道如何提出这个问题。
答案 0 :(得分:0)
首先,您需要DispatcherTimer
,而且必须使用StackPanel
代替Grid
。您还需要在public static bool
上声明Window1
。然后,当点击Window2
上的按钮时,bool
设置为True
,这意味着执行一个空格,将对象添加到StackPanel
或{{ 1}}
的 Window1.xaml 强>
首先,您需要将Scroll Viewer
更改为Grid
。假设您的StackPanel
名称为ScrollViewer
,scrollViewer1
,则应尝试以下内容
更改以下内容
Margin = "37,36,58,36"
到
<Grid>
<ScrollViewer Margin="37,36,58,36" Name="scrollViewer1" />
</Grid>
这样您就可以使用<StackPanel x:Name="stackPanel">
<ScrollViewer Margin="37,36,58,36" Name="scrollViewer1" />
</StackPanel>
代替StackPanel
名称Grid
的 Window1.xaml 强>
stackPanel
的 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.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();
}
}
}
}
谢谢, 我希望你觉得这很有帮助:)
答案 1 :(得分:0)
执行此操作的正确方法是让您的第二个表单有一个custom Event,将信息传递给您的MainWindow。
这是概念的快速证明,看看它是否适合您。
<强> MainWindow.xaml.cs 强>
滚动查看器中的1个scrollviewer 1 stackpanel和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; }
}
}
}