我正在开发一个C#项目,其中必须使用WPF,并且在将动态内容绑定到UI时遇到一些问题。
简而言之,我在一个名为NotifyIconResources.xaml的文件中声明了此textBlock:
<TextBlock Name="label_stato" Text="{Binding Source={x:Static Application.Current}, Path=Properties[status]}"/>
它显示了在App.xaml.cs中声明的名为status的字符串属性的值:
public partial class App : Application, INotifyPropertyChanged
{
private MainWindow mw;
private TaskbarIcon notifyIcon;
public event PropertyChangedEventHandler PropertyChanged;
private string _status;
private string status
{
get
{
return _status;
}
set
{
_status = value;
NotifyPropertyChanged("status");
}
}
/// <summary>
/// Triggers a GUI update on a property change
/// </summary>
/// <param name="propertyName"></param>
private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.Properties["status"] = "Online";
//create the notifyicon (it's a resource declared in NotifyIconResources.xaml
ResourceDictionary rd = new ResourceDictionary
{
Source = new Uri("/NotifyIconResources.xaml", UriKind.RelativeOrAbsolute)
};
notifyIcon = (TaskbarIcon)rd["NotifyIcon"];
mw = new MainWindow(Environment.GetCommandLineArgs()[1]);
mw.Show();
}
}
该属性的值是动态的,其初始值显示在textBlock中,但是对其值的下一次修改不会导致刷新textBlock内容。
NotifyIconResources.xaml在App.xaml中声明:
<Application x:Class="FileSharingOnLAN.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FileSharingOnLAN"
StartupUri="MainWindow.xaml"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="NotifyIconResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
<Application.MainWindow>
<NavigationWindow Source="MainWindow.xaml" Visibility="Visible"></NavigationWindow>
</Application.MainWindow>
NotifyIconResources.xaml文件:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FileSharingOnLAN"
xmlns:tb="http://www.hardcodet.net/taskbar">
<tb:TaskbarIcon x:Key="NotifyIcon"
IconSource="/Red.ico"
ToolTipText="Click for popup, right-click for menu"
ContextMenu="{StaticResource SysTrayMenu}">
<tb:TaskbarIcon.TrayPopup>
<Border
Background="Black"
Width="200"
Height="100">
<StackPanel>
<Button
Content="{Binding ButtonContent}"
VerticalAlignment="Top"
Command="{Binding ShowImpostazioniWindowCommand}"/>
<Border
Background="Blue"
Width="100"
Height="50"
VerticalAlignment="Bottom"
HorizontalAlignment="Left">
<StackPanel>
<Label
Content="Stato" />
<!--<Button
Content="{Binding Path="status", Source="{x:Static Application.Current}"}"
Command="{Binding StatusClickedCommand}"/>-->
<TextBlock Name="label_stato" Text="{Binding Source={x:Static Application.Current}, Path=Properties[status]}"/>
</StackPanel>
</Border>
</StackPanel>
</Border>
</tb:TaskbarIcon.TrayPopup>
<tb:TaskbarIcon.DataContext>
<local:NotifyIconViewModel />
</tb:TaskbarIcon.DataContext>
</tb:TaskbarIcon>
我试图通过使用OnpropertyChanged来修复它,例如:Binding to change the property,但是它不起作用。
在同一项目中,我使用了其他有效的绑定技术,并且我知道如何以编程方式解决此问题,但是我想了解我的错误在哪里,我完全陷入其中。
答案 0 :(得分:1)
您需要更改的几件事:
第一个是其自身的状态属性,将其公开:
public string Status
{
get
{
return (string)this.Properties["status"];
}
set
{
this.Properties["status"] = value;
NotifyPropertyChanged("Status");
}
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Status = "Online";
...
}
并将绑定更改为:
<TextBlock Text="{Binding Source={x:Static Application.Current}, Path=Status}" />
工作示例:
<Window x:Class="WpfApplicationTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplicationTest"
mc:Ignorable="d"
x:Name="win"
Title="MainWindow" Height="300" Width="300">
<Window.Resources>
</Window.Resources>
<StackPanel FocusManager.FocusedElement="{Binding ElementName=btnDefault}">
<TextBox Text="{Binding Source={x:Static Application.Current}, Path=Status}" />
<TextBlock Text="{Binding Source={x:Static Application.Current}, Path=Status}" />
<Button>Button 7</Button>
</StackPanel>
</Window>
答案 1 :(得分:0)
找到了可行的解决方案。
我正在像这样更改另一个cs模块中的属性值:
Application.Current.Properties["status"] = "Offline";
此行不能导致该属性的修改,因此我没有对其进行修改。因此,感谢https://stackoverflow.com/a/1609155/10173298,我用以下行代替了它:
((App)Application.Current).setOffline();
setOffline()是我添加到App.xaml.cs中的一种方法:
public void setOffline()
{
this.Status = "Offline";
}