[英]WPF binding works only once
我正在开发一个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内容的刷新。
在App.xaml中声明了NotifyIconResources.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来修复它,如下所示: 绑定以更改属性,但是没有用。
在同一项目中,我使用了其他有效的绑定技术,并且我知道如何以编程方式解决此问题,但我想了解我的错误在哪里,我完全陷入了困境。
您有几件事需要更改:
第一个是自己的状态属性,将其公开:
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>
找到了可行的解决方案。
我正在像这样更改另一个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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.