使用以下代码我获得带有上下文菜单的系统托盘图标。但是当我在应用程序运行时更改Windows主题时,背景颜色保持不变。
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");
}
这里是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>
我无法理解这一点,我已经使用Style =“{x:Null}”来摆脱所有错过配置的东西,但它根本不起作用。我可以避免使用System.Windows.Controls.Contextmenu,但我应该使用什么呢?
我很感谢所有提示。
由于
答案 0 :(得分:2)
如果您想在更改窗口颜色时更改颜色,则控件需要使用其中一种系统颜色System colors in wpf
然后使用dynamicresource以便在更改资源时重新应用资源
<Border Width="100" Height="100" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
答案 1 :(得分:0)
我将受影响的XAML代码更改为:
<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>
但结果仍然相同。
通常情况下,这可以解决问题,但是如果有托盘图上下文菜单,则会失败。