3

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

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

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

4

2 回答 2

0

首先,您需要 aDispatcherTimer并且必须使用StackPanel而不是Grid. 您还需要声明一个public static boolon Window1。然后,当单击 on 按钮时Window2bool设置为True这意味着执行一个 void 将对象添加StackPanelScroll Viewer

XAML

Window1.xaml

首先,您需要更改GridStackPanel. 假设您有一个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>

这样您就可以使用 aStackPanel而不是 aGrid的名称stackPanel  


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;
        }
    }
}

谢谢,
我希望你觉得这有帮助:)

于 2012-10-27T02:41:42.417 回答
0

执行此操作的正确方法是让您的第二个表单拥有一个自定义事件,将信息传递到您的 MainWindow。

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

MainWindow.xaml.cs
1 scrollviewer 1 stackpanel 在 scrollviewer 和 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; }
        }
    }
}
于 2012-10-27T02:59:16.270 回答