我的小WPF托盘图标有问题,该图标已分配上下文菜单。如果我右键单击托盘图标,则会打开上下文菜单,您会看到一个包含整数值的文本框。现在点击"创建实例"字段整数应该加1。新值应显示在文本框中。启动时,文本框中会显示正确的初始值,但运行期间的更改不会反映到UI。我认为存在数据绑定问题...捕获了click事件,甚至值增加但文本框无法识别新值......
我没有主窗口,应用程序应该只是一个托盘图标上下文菜单。
App.xaml.cs
public partial class App : Application
{
// This objects are global
private TaskbarIcon notifyIcon;
private INotificationBarMessage message = null;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//create the notifyicon (a resource declared in NotifyIconResources.xaml)
notifyIcon = (TaskbarIcon) FindResource("Key_NotifyIcon");
notifyIcon.DataContext = this;
message = new NotificationBarMessage(notifyIcon);
message.Show(TextType.Startup);
}
}
ResourceContextMenu.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
xmlns:local="clr-namespace:UserInterface.ViewModel"
x:Class="UserInterface.ViewModel.EventHandlerUI">
<ContextMenu x:Shared="false" x:Key="My_ContextMenu">
<ContextMenu.DataContext>
<local:ContextMenuModel /> <!-- reference to ContextMenuModel.cs -->
</ContextMenu.DataContext>
<!-- DataContext="{Binding PlacementTarget.DataContext,
RelativeSource={RelativeSource Self}} -->
<ContextMenu.Resources>
<local:ConverterBoolToVisibility x:Key="BoolToHiddenConverter"
TrueValue="Visible"
FalseValue="Hidden" />
<local:ConverterIntToString x:Key="IntToStringConverter" />
</ContextMenu.Resources>
<TextBlock Text="Status"/>
<Separator/>
<MenuItem Header="Create Instance"
MouseEnter="CreateInstance_OverEnter"
MouseLeave="CreateInstance_OverExit"
Click="CreateInstance_Click"
StaysOpenOnClick="True" />
<!-- Command="{Binding ShowWindowCommand}" -->
<MenuItem Header="Open Window"
StaysOpenOnClick="True"
Command="{Binding Commands.ShowWindowCommand}" />
<MenuItem Header="IP-Address"
Name="MenuItem_IPAddress"
Visibility="{Binding EventData.IsEnabled, Mode=TwoWay,
Converter={StaticResource BoolToHiddenConverter}}"
StaysOpenOnClick="True"/>
<TextBox Width="100"
Text="{Binding Path=EventData.Counter,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,
Converter={StaticResource IntToStringConverter}}"/>
<Separator/>
<MenuItem Header="Exit"
Command="{Binding Path=Commands.ExitApplicationCommand}" />
</ContextMenu>
<!-- Tray Icon -->
<!-- the application's NotifyIcon - started from App.xaml.cs.
Declares its own view model. -->
<tb:TaskbarIcon x:Key="Key_NotifyIcon"
x:Name="My_NotifyIcon"
IconSource="/View/gfx/_UserInterface.ico"
ToolTipText="tip"
ContextMenu="{StaticResource My_ContextMenu}"/>
的App.xaml
<Application x:Class="UserInterface.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
<!-- Note that this application does not have a StartupUri declared,
so no Window is automatically loaded.
Also, the ShutdownMode was set to explicit,
so we have to close the application programmatically. -->
<!-- merge NotifyIcon and related stuff into the application -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceContextMenu.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
以下是我触发属性更改事件的代码:
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private int _counter = 34;
public int Counter
{
get
{
return this._counter;
}
set
{
if (value != this._counter)
{
this._counter = value;
NotifyPropertyChanged();
}
}
}
任何帮助都将不胜感激。