Ingo Bingo
Ingo Bingo

Reputation: 11

Background Color doesn't change while changing windows system colors

with the following code I get an system tray icon with context menu. But when I change the windows theme while the application is running, than the background color stays unchanged.

private System.Windows.Forms.NotifyIcon notifyIcon1;
    private System.Windows.Controls.ContextMenu contextMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.ComponentModel.IContainer components;


    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        Action action = new Action(ExecuteStartupSequence);
        action.ExecuteProfiled();


        this.components = new System.ComponentModel.Container();
        /*
        this.contextMenu1 = new System.Windows.Forms.ContextMenu();
        this.menuItem1 = new System.Windows.Forms.MenuItem();
         */

        // Initialize contextMenu1
        /*
        this.contextMenu1.MenuItems.AddRange(
                    new System.Windows.Forms.MenuItem[] { this.menuItem1 });
        */

        // Initialize menuItem1
        /*
        this.menuItem1.Index = 0;
        this.menuItem1.Text = "E&xit";
        this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
        */
        this.contextMenu1 = this.FindResource("TrayContextMenu") as System.Windows.Controls.ContextMenu;


        // Create the NotifyIcon.
        //this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
        this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(components);

        // The Icon property sets the icon that will appear
        // in the systray for this application.
        notifyIcon1.Icon = new System.Drawing.Icon("Icon1.ico");

        // The ContextMenu property sets the menu that will
        // appear when the systray icon is right clicked.
        // notifyIcon1.ContextMenu = this.contextMenu1;

        // The Text property sets the text that will be displayed,
        // in a tooltip, when the mouse hovers over the systray icon.
        notifyIcon1.Text = "Form1 (NotifyIcon example)";
        notifyIcon1.Visible = true;

        // Handle the DoubleClick event to activate the form.
        notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
        notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_DoubleClick);



        //tb = (TaskbarIcon)FindResource("notifyIcon"); ;
    }

    private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
    {
        this.contextMenu1 = this.FindResource("TrayContextMenu") as System.Windows.Controls.ContextMenu;
        contextMenu1.IsOpen = true;
    }

    private void menuItem1_Click(object Sender, EventArgs e)
    {
        MessageBox.Show("Open");


    }

And here the XAML-Stuff.

<ContextMenu x:Key="TrayContextMenu" Placement="MousePoint" Style="{x:Null}">
        <MenuItem Header="First Menu Item" Style="{x:Null}" />
        <MenuItem Header="Second Menu Item" Style="{x:Null}" />
        </ContextMenu>
        <Popup x:Key="TrayPopup" Placement="MousePoint">
            <Border Width="100" Height="100" Background="White" BorderBrush="Orange" BorderThickness="4">
            <Button Content="Close" Click="menuItem1_Click"></Button>
            </Border>
        </Popup>

I can't understand this, I already use Style="{x:Null}", to get rid of all miss configured stuff, but it simply doesn't work. May I have to avoid using System.Windows.Controls.Contextmenu, but what should I use instead?

I'm thankful for all hints.

Thanks

Upvotes: 1

Views: 1505

Answers (2)

Ingo Bingo
Ingo Bingo

Reputation: 11

I changed the affected XAML-code to:

<ContextMenu 
        x:Key="TrayContextMenu" 
        Placement="MousePoint" 
        Style="{x:Null}" 
        Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
        <MenuItem 
            Header="First Menu Item" 
            Style="{x:Null}"
            Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>

        <MenuItem 
            Header="Second Menu Item" 
            Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />

        </ContextMenu>
        <Popup x:Key="TrayPopup" Placement="MousePoint">
            <Border Width="100" Height="100" Background="White" BorderBrush="Orange" BorderThickness="4">
            <Button Content="Close" Click="menuItem1_Click"></Button>
            </Border>
        </Popup>

But the result was still the same.

Normally this would solve the problem, but in case of an trayicon contextmenu it fails.

Upvotes: 0

Sebastian &#208;ymel
Sebastian &#208;ymel

Reputation: 717

If you want to get color changed when you change windows colors, than your control need to use one of the system colors System colors in wpf

Then use dynamicresource in order to reapply resource when it get changed

 <Border Width="100" Height="100" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"

Upvotes: 2

Related Questions