WPF C# PropertyChanged 始终为空
WPF C# PropertyChanged always null
我已经尝试了一段时间并决定看看是否有人可以提供帮助,我在 StatusInfo 的构造函数中设置了 DataContext = this 但没有用。当我将字符串写入 ScreenStatusBarText 时,它会调用 OnPropertyChanged 方法,但每次 PropertyChanged 值为 null。我在屏幕底部的状态块。我在这个堆栈面板上方有一个选项卡部分,其中包含许多使用绑定和工作的组件。
屏幕代码
<StackPanel Margin="0,1047,0,0">
<Grid Name="StatusBarItemGrid">
<TextBlock Name="StatusBarText" Text="may the force be with you" VerticalAlignment="Center" HorizontalAlignment="Stretch" />
</Grid>
</StackPanel>
数据模型:
public partial class StatusInfo : INotifyPropertyChanged
{
private string screenStatusBarText;
public StatusInfo()
{
BindScreenStatusBarText();
screenStatusBarText = "Initialized";
}
public string ScreenStatusBarText
{
get { return screenStatusBarText; }
set
{
screenStatusBarText = value;
OnPropertyChanged("StatusBarText");
}
}
private void BindScreenStatusBarText()
{
Binding b = new Binding();
b.Source = screenStatusBarText;
b.Mode = BindingMode.OneWay;
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
b.Path = new PropertyPath("StatusBarText");
MainWindow.mainWindow.StatusBarText.SetBinding(TextBlock.TextProperty, b);
MainWindow.mainWindow.StatusBarText.DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(
this, new PropertyChangedEventArgs(propName));
}
}
我的主要 :
public partial class MainWindow : Window
{
public static StatusInfo status;
public MainWindow()
{
InitializeComponent();
SourceInitialized += MainWindow_SourceInitialized;
}
private void MainWindow_SourceInitialized(object sender, EventArgs e)
{
SetUpDisplay();
}
private void SetUpDisplay()
{
status = new StatusInfo();
}
}
我认为您将很难在代码隐藏中进行绑定。
话说回来,关于为什么你的PropertyChanged
值为null。您只是打错了字,as-is 您是在通知订阅者不存在的 属性 已更改。避免此类拼写错误的一种解决方案是使用 nameof
。
public string ScreenStatusBarText
{
get { return screenStatusBarText; }
set
{
screenStatusBarText = value;
OnPropertyChanged(nameof(ScreenStatusBarText));
}
}
我突然想到你可能也意味着你的活动是空的。这仅仅意味着您没有任何订阅者。参见 Why is my "Event" always null?。
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) // I have a subscriber.
handler(this, new PropertyChangedEventArgs(propertyName));
}
在 XAML 中设置绑定而不是隐藏代码:
<TextBlock Text="{Binding ScreenStatusBarText}" />
并使用像
这样的视图模型
public class StatusInfo : INotifyPropertyChanged
{
private string screenStatusBarText = "Initialized";
public string ScreenStatusBarText
{
get { return screenStatusBarText; }
set
{
screenStatusBarText = value;
OnPropertyChanged(nameof(ScreenStatusBarText));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this,
new PropertyChangedEventArgs(propertyName));
}
}
将视图模型的实例 class 分配给 MainWindow 的 DataContext:
private readonly StatusInfo statusInfo = new StatusInfo();
public MainWindow()
{
InitializeComponent();
DataContext = statusInfo;
}
您现在可以在以后随时访问视图模型 class,例如在 MainWindow 的一个元素的事件处理程序中:
statusInfo.ScreenStatusBarText = "Something";
我已经尝试了一段时间并决定看看是否有人可以提供帮助,我在 StatusInfo 的构造函数中设置了 DataContext = this 但没有用。当我将字符串写入 ScreenStatusBarText 时,它会调用 OnPropertyChanged 方法,但每次 PropertyChanged 值为 null。我在屏幕底部的状态块。我在这个堆栈面板上方有一个选项卡部分,其中包含许多使用绑定和工作的组件。
屏幕代码
<StackPanel Margin="0,1047,0,0">
<Grid Name="StatusBarItemGrid">
<TextBlock Name="StatusBarText" Text="may the force be with you" VerticalAlignment="Center" HorizontalAlignment="Stretch" />
</Grid>
</StackPanel>
数据模型:
public partial class StatusInfo : INotifyPropertyChanged
{
private string screenStatusBarText;
public StatusInfo()
{
BindScreenStatusBarText();
screenStatusBarText = "Initialized";
}
public string ScreenStatusBarText
{
get { return screenStatusBarText; }
set
{
screenStatusBarText = value;
OnPropertyChanged("StatusBarText");
}
}
private void BindScreenStatusBarText()
{
Binding b = new Binding();
b.Source = screenStatusBarText;
b.Mode = BindingMode.OneWay;
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
b.Path = new PropertyPath("StatusBarText");
MainWindow.mainWindow.StatusBarText.SetBinding(TextBlock.TextProperty, b);
MainWindow.mainWindow.StatusBarText.DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(
this, new PropertyChangedEventArgs(propName));
}
}
我的主要 :
public partial class MainWindow : Window
{
public static StatusInfo status;
public MainWindow()
{
InitializeComponent();
SourceInitialized += MainWindow_SourceInitialized;
}
private void MainWindow_SourceInitialized(object sender, EventArgs e)
{
SetUpDisplay();
}
private void SetUpDisplay()
{
status = new StatusInfo();
}
}
我认为您将很难在代码隐藏中进行绑定。
话说回来,关于为什么你的PropertyChanged
值为null。您只是打错了字,as-is 您是在通知订阅者不存在的 属性 已更改。避免此类拼写错误的一种解决方案是使用 nameof
。
public string ScreenStatusBarText
{
get { return screenStatusBarText; }
set
{
screenStatusBarText = value;
OnPropertyChanged(nameof(ScreenStatusBarText));
}
}
我突然想到你可能也意味着你的活动是空的。这仅仅意味着您没有任何订阅者。参见 Why is my "Event" always null?。
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) // I have a subscriber.
handler(this, new PropertyChangedEventArgs(propertyName));
}
在 XAML 中设置绑定而不是隐藏代码:
<TextBlock Text="{Binding ScreenStatusBarText}" />
并使用像
这样的视图模型public class StatusInfo : INotifyPropertyChanged
{
private string screenStatusBarText = "Initialized";
public string ScreenStatusBarText
{
get { return screenStatusBarText; }
set
{
screenStatusBarText = value;
OnPropertyChanged(nameof(ScreenStatusBarText));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this,
new PropertyChangedEventArgs(propertyName));
}
}
将视图模型的实例 class 分配给 MainWindow 的 DataContext:
private readonly StatusInfo statusInfo = new StatusInfo();
public MainWindow()
{
InitializeComponent();
DataContext = statusInfo;
}
您现在可以在以后随时访问视图模型 class,例如在 MainWindow 的一个元素的事件处理程序中:
statusInfo.ScreenStatusBarText = "Something";