Set Style in code behind

Jan 19, 2011 at 4:16 PM

I am implemenitng the popup menu on a datagrid through code behind and I can't seem to apply a Style.  I have the following Style defined in App:

<Style x:Key="PopupMenuStyle1" TargetType="Control">
            <Setter Property="Background" Value="#FFCABE93"/>
            <Setter Property="Foreground" Value="DarkSlateBlue"/>
When I create the menu in code I apply the style...
private void Createmenu()
                SL4PopupMenu.PopupMenu menu = new SL4PopupMenu.PopupMenu();
                menu.AddItem("Rename", delegate { RenameRegion(RegionDataGrid.SelectedItem); });
                menu.AddItem("Delete", delegate { DeleteRegion(RegionDataGrid.SelectedItem); });

                menu.PopupMenuItem(0).Tooltip = "Rename Region.";
                menu.PopupMenuItem(2).Tooltip = "Delete Region.";
                menu.PopupMenuItem(0).Foreground = new SolidColorBrush(Colors.Green);// no worky
                menu.PopupMenuItem(2).Foreground = new SolidColorBrush(Colors.Green);

                menu.AddTrigger(SL4PopupMenu.TriggerTypes.RightClick, RegionDataGrid);

                //tried applying to both...
                Style st = App.Current.Resources["PopupMenuStyle1"] as Style;
                menu.Style = st;
                menu.PopupMenuItem(0).Style = st;

Setting each item's Forground does not work and applying the Style does not work either - any ideas?
Mar 7, 2011 at 2:12 AM

Your code is working fine on my machine. Are you using the latest source code? You can download it from here: