0

我阅读了 WPF4 unleashed book 的事件部分,但没有找到我要找的东西。

例如我有两个窗口都已经打开)。

我在第一个按钮上有一个按钮,在第二个按钮上有一个文本框(以数字作为内容)。

每次单击按钮时,我都希望文本框的数量增加而无需再次打开第二个窗口。期待您的帮助和建议!

我从主窗口打开了 2 个窗口,并在第一个窗口的按钮 click_event 上尝试了此代码:

    private void b_Click(object sender, RoutedEventArgs e)
    {
        int i = int.Parse(WpfApplication22.Window2.textbox.Text);
        i++;
        WpfApplication22.Window1.textbox.Text = i.ToString();            
    }
4

3 回答 3

2

我建议您使用自己的自定义事件处理程序来做这种事情。对于您的问题所需要的内容,这可能有点矫枉过正,但它有更多的用途和更多的可扩展性。WPF 也应该使用一种 MVVM 模式来实现,这意味着您只需要让 ViewModel 相互交谈。出于本示例的目的,我将假设每个窗口都有一个关联的 ViewModel。

首先创建您自己的事件参数,如下图所示。

public class MyCustomEventArgs : EventArgs {

}

然后编写自己的事件处理程序类,如下所示。

public class CustomEventHander {
  static public event EventHandler CustomEvent;

  static public void RaiseMyCustomEvent(object sender, EventArgs args) {
    if (CustomEvent != null) CustomEvent(sender,args);
  }
}

然后你只需要把它连接起来。

在您的第一个 Windows ViewModel 上,您需要将事件处理程序放在您的构造函数中。

CustomEventHandler.CustomEvent += handleCustomEvent;

其次,您通过编写 handleCustomEvent 方法来编写事件的句柄。这是您增加所需数字的地方。

private void handleCustomEvent(object sender, EventArgs e) {
  if (e is MyCustomEventArgs) {
    this.IntValue += 1;
  }
}

到目前为止,我们可以处理事件,引发事件,但我们需要触发事件。

在第二个 Windows 控制器中,将 ICommand 连接到按钮并将以下代码放入方法中。

CustomEventHandler.RaiseMyCustomEvent(this, new MyCustomEventArgs());

我希望这回答了你的问题。当然,我已经假设使用 MVVM 并且属性 IntValue 被绑定到第一个窗口上的文本框。但是此代码应该有望满足您的需求。

于 2013-04-15T01:40:27.297 回答
1

您可以将DataContext第二个分配Window给第一个Window并使用 aDependancyProperty绑定您想要的值

例子:

窗口1代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public int Count
    {
        get { return (int)GetValue(CountProperty); }
        set { SetValue(CountProperty, value); }
    }
    public static readonly DependencyProperty CountProperty =
        DependencyProperty.Register("Count", typeof(int), typeof(MainWindow), new UIPropertyMetadata(0));

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //SetBinding windows DataContext to this window
        new PopupWindow { DataContext = this }.Show();
    }
}

Window1 Xaml:

<Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="325" Width="422" Name="UI">
    <Grid DataContext="{Binding ElementName=UI}">
        <StackPanel>
            <TextBox Text="{Binding Count, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
            <Button Content="Open Window 2" Click="Button_Click" />
        </StackPanel>
    </Grid>
</Window>

窗口2代码:

public partial class PopupWindow : Window
{
    public PopupWindow()
    {
        InitializeComponent();
    }
}

Window2 Xaml:

<Window x:Class="WpfApplication10.PopupWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication10"
        Title="PopupWindow" Height="300" Width="300">
    <Grid>
        <TextBlock Text="{Binding Path=Count}" />
    </Grid>
</Window>

结果:

在此处输入图像描述

于 2013-04-15T01:47:43.093 回答
0

您的第一个窗口需要对第二个窗口的引用。

abc = New Window2
abc.Show()

然后,当您的按钮的单击事件触发时,您可以执行此操作。

abc.TextBox1.Text = CStr(CInt(abc.TextBox1.Text) + 1) 
于 2013-04-15T01:03:55.710 回答