Amine Da.
Amine Da.

Reputation: 1164

assign an event to another wpf window

I read the event part of the WPF4 unleashed book but I didn't find what I'm looking for.

For example I have two windows (both are already opened).

I have a button on the first one and a textbox in the second (with a number as a content) .

Everytime I click the button I want the textbox's number increases without opening the second window again. Looking forward to your help and suggestions!

I've opened the 2 windows from the mainwindow and I tried this code on the button click_event of the first window :

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

Upvotes: 0

Views: 4361

Answers (3)

sa_ddam213
sa_ddam213

Reputation: 43626

You can just assign the DataContext on the second Window to the first Window and use a DependancyProperty to bind the value you want

Example:

Window1 Code:

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>

Window2 Code:

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>

Result:

enter image description here

Upvotes: 1

sepai
sepai

Reputation: 166

I would suggest you roll your own custom event handler to do this kind of thing. It may be a bit of overkill for what your question requires but it has a lot more uses and far more extensible. Also WPF should be implemented using an MVVM pattern meaning you just need to get the ViewModels talking to one another. For the purpose on this example I will assume each window has an associated ViewModel.

First start by creating your own event arguments as illustrated below.

public class MyCustomEventArgs : EventArgs {

}

Then write your own event handler class as follows.

public class CustomEventHander {
  static public event EventHandler CustomEvent;

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

Then you just need to wire it up.

On your first windows ViewModel you would need to put the event handler in your constructor.

CustomEventHandler.CustomEvent += handleCustomEvent;

Secondly you write the handle the event by writing the handleCustomEvent method. This is where you you increment the number you need.

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

So far we can handle the events, raise the events but we need to trigger the event.

In the second windows controller wire an ICommand to the button and put the following code in the method.

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

I hope this answers your question. Of course I have made an assumption on the use of MVVM and the property IntValue being bound to your text box on the first window. But this code should hopefully suit your needs.

Upvotes: 3

Scott Savage
Scott Savage

Reputation: 373

Your first window needs a reference to the second.

abc = New Window2
abc.Show()

Then, when the click event for your button fires, you can do this.

abc.TextBox1.Text = CStr(CInt(abc.TextBox1.Text) + 1) 

Upvotes: 0

Related Questions