[英]WPF binding works only once
I'm developing a C# project in which I've to use WPF and I'm encountering some problems with binding dynamic content to UI. 我正在开发一个C#项目,其中必须使用WPF,并且在将动态内容绑定到UI时遇到一些问题。
Briefly, I have this textBlock here declared in a file called NotifyIconResources.xaml: 简要地说,我在一个名为NotifyIconResources.xaml的文件中声明了此textBlock:
<TextBlock Name="label_stato" Text="{Binding Source={x:Static Application.Current}, Path=Properties[status]}"/>
It shows the value of a string property, called status, declared in App.xaml.cs: 它显示了在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();
}
}
The value of the property is dynamic, its initial value is showed in the textBlock, but the next modifications of its value don't result in a refresh of the textBlock content. 该属性的值是动态的,其初始值显示在textBlock中,但是对其值的下一个修改不会导致textBlock内容的刷新。
NotifyIconResources.xaml is declared in App.xaml: 在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>
The NotifyIconResources.xaml file: 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>
I've tried to fix it by using OnpropertyChanged, like in this: Binding to change the property but it didn't work. 我试图通过使用OnpropertyChanged来修复它,如下所示: 绑定以更改属性,但是没有用。
In the same project I use other binding techniques which work and I know how to solve this problem programmatically but I want to understand where my error is, I'm totally stuck with it. 在同一项目中,我使用了其他有效的绑定技术,并且我知道如何以编程方式解决此问题,但我想了解我的错误在哪里,我完全陷入了困境。
You have few things that needs to be changed: 您有几件事需要更改:
First one is status property it self, make it public: 第一个是自己的状态属性,将其公开:
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";
...
}
And change your binding to: 并将绑定更改为:
<TextBlock Text="{Binding Source={x:Static Application.Current}, Path=Status}" />
Working Example: 工作示例:
<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>
Found a working solution. 找到了可行的解决方案。
I was changing the value of the property in another cs module like this: 我正在像这样更改另一个cs模块中的属性值:
Application.Current.Properties["status"] = "Offline";
This line cannot result in a modification of the property, so I wasn't modifying it. 该行不能导致该属性的修改,因此我没有对其进行修改。 So, thanks to https://stackoverflow.com/a/1609155/10173298 , I solved it by substituting that line with:
因此,感谢https://stackoverflow.com/a/1609155/10173298 ,我通过用以下代码替换了这一行来解决了该问题:
((App)Application.Current).setOffline();
Where setOffline() is a method I've added to App.xaml.cs: setOffline()是我添加到App.xaml.cs中的一种方法:
public void setOffline()
{
this.Status = "Offline";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.