1

Closed

Support binding for XAML in NotifyIcon ContextMenu

description


Not working code (binding in xaml):

 
<av:NotifyIcon Icon="my.ico" Text="NotifyIcon Demo">
<FrameworkElement.ContextMenu>
  <ContextMenu x:Name="menu">
    <MenuItem Header="Open" Command="{Binding OpenCommand}" />
    <MenuItem Header="E_xit" Command="{Binding CloseCommand}" />
  </ContextMenu>
</FrameworkElement.ContextMenu>
</av:NotifyIcon>

 

Workarround: (updates framework element context menu data context)

 
public NotifyIcon {
...
 
// ADDED
this.DataContextChanged += new DependencyPropertyChangedEventHandler(OnDataContextChanged);
}
 
#region private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e);
/// <summary>
/// Called when data context changed to update framework element context menu data context.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
if(FrameworkElement.ContextMenuProperty != null) {
  var menu = GetValue(FrameworkElement.ContextMenuProperty) as ContextMenu;
  if(menu != null) {
    menu.DataContext = e.NewValue;
  }
}
}
#endregion
Closed Jul 14, 2009 at 7:34 AM by aelij

comments