2
Answers

WPF Routed Event not firing?

Rob Collins

Rob Collins

13y
5.3k
1
I'm having some difficulty trying to use RoutedEvents. I'd guess this one here is easy for someone who has already done it. I'm making a 'paged' application where I'd like a completed action on one of the pages (one container) to allow changing of some content on another container or the main Window. So based on what I've read (been studying this stuff for 4 months now but apparently that's not enough), to do it correctly I should use 'RoutedEvents' to fire an event from the 'page' that goes up the container hierarchy to the main window. From there I can simply set the content within the other container. In any case, the source and target containers aren't in the same container 'chain'. I've put all what seems like the right code in and there are no errors except the event never gets fired in the main window. The problem seems all in the code behind (event not firing?). I'm not sure but I believe I've set up the handler and listener in an acceptable way based on everything I can find in reading the msdn. All the code gets executed (based on tracing through with breakpoints) but the UpdateStatusBar() method on the main window never gets fired when the RaiseEvent is triggered in the SelectDataset() method within the page. Perhaps I'm missing some fundamental understanding about the WPF definition of "point A to point B"?

For easy viewing, I've only posted the meaningful parts of the code. Here is what I have so far:

//*** page where RoutedEvent is set up and fired from
namespace BlahBlah
{

  public partial class pageCfgBrushes : Page
  {
  // Register routed event - Bubble up
  public static readonly RoutedEvent BrushSelectionChangedEvent =
  EventManager.RegisterRoutedEvent("BrushSelectionChanged", RoutingStrategy.Bubble,
  typeof(RoutedEventHandler), typeof(pageCfgBrushes));

  public event RoutedEventHandler BrushSelectionChanged
  {
  add { AddHandler(BrushSelectionChangedEvent, value); }
  remove { RemoveHandler(BrushSelectionChangedEvent, value); }
  }

  public pageCfgBrushes()
  {
  InitializeComponent();
}

  //*** The SelectDataset is fired from a comboListbox change event on the page, and it does run this code
  private void SelectDataset(object sender, SelectionChangedEventArgs e)
  {

  // Raise the routed event "BrushSelectionChangedEvent"
  RaiseEvent(new RoutedEventArgs(BrushSelectionChangedEvent));
 
  }
  }
}




//*** MainWindow
namespace BlahBlah
{
  public partial class MainWindow : Window
  {
  public MainWindow()
  {
  InitializeComponent();  //Initializes XAML code
  }

  protected override void OnInitialized(EventArgs e)
  {
  base.OnInitialized(e);


  //Don't show the navigation bar on screen
  navContent.NavigationUIVisibility = NavigationUIVisibility.Hidden;
  //...

  //Display main page on load
  navContent.Navigate(new pageMain());

  //testing RoutedEvent
  pnlStatusArea.AddHandler(pageCfgBrushes.BrushSelectionChangedEvent, new RoutedEventHandler(UpdateStatusBar));

  }

  //*** But this UpdateStatusBar routine never gets run
  private void UpdateStatusBar(object sender, RoutedEventArgs e)
  {
  this.StatusBlockMsg.Text = "DataChanged Event";
  }
  }
}
 
Answers (2)