ScrollViewer を備えた WPF アプリケーション (ウィンドウ 1) があります。ボタンがある同じ WPF アプリケーションの別の 2 番目のウィンドウがあります。
2 番目のウィンドウのボタンをクリックすると、最初のウィンドウの ScrollViewer に項目が追加されます。
どうすればこれを達成できますか? これが曖昧で申し訳ありませんが、この質問の仕方がわかりません。
まず、 が必要で、代わりにDispatcherTimerを使用する必要があります。onも宣言する必要があります。次に、 のボタンがクリックされると、オブジェクトを に追加する void を実行する手段が に設定されます。StackPanelGridpublic static boolWindow1Window2boolTrueStackPanelScroll Viewer
Window1.xaml
Gridまず、 に変更する必要がありますStackPanel。、およびScrollViewerという名前の があると仮定すると、次のことを試してください。scrollViewer1Margin = "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代わりに aを使用できるようにGridstackPanel
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 番目のフォームに、情報を 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; }
}
}
}