WPF 双向绑定到静态属性
WPF two-way binding to static properties
我目前在 WPF 4.6.1 中遇到双向绑定问题。它根本不起作用,我使用了这个例子。
https://www.c-sharpcorner.com/UploadFile/mahesh/binding-static-properties-in-wpf-4-5/
我的项目有两个 windows(在第一个 window 上按下 TextBlock 时打开的数字小键盘和显示数据的表单)。它还有一个静态 class 保存在虚拟键盘上输入的信息。
这是打开小键盘的文本框
<TextBlock Margin="5 10 0 10" x:Name="submittedQty" Background="PaleVioletRed" HorizontalAlignment="Stretch" TextAlignment="Center" Text="{Binding Path=(local:TemporaryData.SubmittedQuantity), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="22" FontWeight="Bold" MouseLeftButtonDown="submittedQty_MouseLeftButtonDown"/>
这是我保存信息的静态 class。
static class TemporaryData //Static Class
{
private static string _SubmittedQuantity;
private static string _ConfirmedQuantity;
public static event EventHandler QuantityChanged;
public static string SubmittedQuantity {
get => _SubmittedQuantity;
set
{
if (value != _SubmittedQuantity)
{
_SubmittedQuantity = value;
if (QuantityChanged != null)
QuantityChanged(null, EventArgs.Empty);
}
}
}
public static string ConfirmedQuantity {
get => _ConfirmedQuantity;
set
{
if (value != _ConfirmedQuantity)
{
_ConfirmedQuantity = value;
if (QuantityChanged != null)
{
QuantityChanged(null, EventArgs.Empty);
}
}
}
}
在我的虚拟数字键盘上按下 OK 后,我执行此方法:
private void okBtn_Click(object sender, RoutedEventArgs e)
{
models.TemporaryData.ConfirmedQuantity = typedTextTxtBox.Text;
this.Close();
}
运行 调试器我注意到,当我的值在虚拟键盘上更新时,行
if (QuantityChanged != null)
被跳过,因为 QuantityChanged 为空。
谁能告诉我为什么当静态 class 的值发生变化时文本块不更新?
谢谢
按照 Clemens 的建议,我更新了我的代码如下:
更新
XML
<TextBlock Margin="5 10 0 10" x:Name="submittedQty" Background="PaleVioletRed" HorizontalAlignment="Stretch" TextAlignment="Center" Text="{Binding Path=(local:TemporaryData.SubmittedQuantity)}" FontSize="22" FontWeight="Bold" MouseLeftButtonDown="submittedQty_MouseLeftButtonDown"/>
静态class
static class TemporaryData
{
private static string _SubmittedQuantity;
private static string _ConfirmedQuantity;
//public static event EventHandler QuantityChanged;
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
public static string SubmittedQuantity {
get => _SubmittedQuantity;
set
{
if (value != _SubmittedQuantity)
{
_SubmittedQuantity = value;
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(nameof(SubmittedQuantity)));
}
}
}
public static string ConfirmedQuantity {
get => _ConfirmedQuantity;
set
{
if (value != _ConfirmedQuantity)
{
_ConfirmedQuantity = value;
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(nameof(ConfirmedQuantity)));
}
}
}
QuantityChanged
事件仅适用于 Quantity
属性。
为了通知 SubmittedQuantity
属性 的更改值,应该有一个 SubmittedQuantityChanged
事件:
public static event EventHandler SubmittedQuantityChanged;
public static string SubmittedQuantity
{
get => _SubmittedQuantity;
set
{
if (value != _SubmittedQuantity)
{
_SubmittedQuantity = value;
SubmittedQuantityChanged?.Invoke(null, EventArgs.Empty);
}
}
}
IMO 更好的选择是对所有静态属性使用通用的 属性 更改事件:
public static event PropertyChangedEventHandler StaticPropertyChanged;
public static string SubmittedQuantity
{
get => _SubmittedQuantity;
set
{
if (value != _SubmittedQuantity)
{
_SubmittedQuantity = value;
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(nameof(SubmittedQuantity)));
}
}
}
有关详细信息,请参阅 What's New in WPF Version 4.5 - Binding to static properties。
我目前在 WPF 4.6.1 中遇到双向绑定问题。它根本不起作用,我使用了这个例子。 https://www.c-sharpcorner.com/UploadFile/mahesh/binding-static-properties-in-wpf-4-5/
我的项目有两个 windows(在第一个 window 上按下 TextBlock 时打开的数字小键盘和显示数据的表单)。它还有一个静态 class 保存在虚拟键盘上输入的信息。
这是打开小键盘的文本框
<TextBlock Margin="5 10 0 10" x:Name="submittedQty" Background="PaleVioletRed" HorizontalAlignment="Stretch" TextAlignment="Center" Text="{Binding Path=(local:TemporaryData.SubmittedQuantity), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="22" FontWeight="Bold" MouseLeftButtonDown="submittedQty_MouseLeftButtonDown"/>
这是我保存信息的静态 class。
static class TemporaryData //Static Class
{
private static string _SubmittedQuantity;
private static string _ConfirmedQuantity;
public static event EventHandler QuantityChanged;
public static string SubmittedQuantity {
get => _SubmittedQuantity;
set
{
if (value != _SubmittedQuantity)
{
_SubmittedQuantity = value;
if (QuantityChanged != null)
QuantityChanged(null, EventArgs.Empty);
}
}
}
public static string ConfirmedQuantity {
get => _ConfirmedQuantity;
set
{
if (value != _ConfirmedQuantity)
{
_ConfirmedQuantity = value;
if (QuantityChanged != null)
{
QuantityChanged(null, EventArgs.Empty);
}
}
}
}
在我的虚拟数字键盘上按下 OK 后,我执行此方法:
private void okBtn_Click(object sender, RoutedEventArgs e)
{
models.TemporaryData.ConfirmedQuantity = typedTextTxtBox.Text;
this.Close();
}
运行 调试器我注意到,当我的值在虚拟键盘上更新时,行
if (QuantityChanged != null)
被跳过,因为 QuantityChanged 为空。
谁能告诉我为什么当静态 class 的值发生变化时文本块不更新?
谢谢
按照 Clemens 的建议,我更新了我的代码如下:
更新 XML
<TextBlock Margin="5 10 0 10" x:Name="submittedQty" Background="PaleVioletRed" HorizontalAlignment="Stretch" TextAlignment="Center" Text="{Binding Path=(local:TemporaryData.SubmittedQuantity)}" FontSize="22" FontWeight="Bold" MouseLeftButtonDown="submittedQty_MouseLeftButtonDown"/>
静态class
static class TemporaryData
{
private static string _SubmittedQuantity;
private static string _ConfirmedQuantity;
//public static event EventHandler QuantityChanged;
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
public static string SubmittedQuantity {
get => _SubmittedQuantity;
set
{
if (value != _SubmittedQuantity)
{
_SubmittedQuantity = value;
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(nameof(SubmittedQuantity)));
}
}
}
public static string ConfirmedQuantity {
get => _ConfirmedQuantity;
set
{
if (value != _ConfirmedQuantity)
{
_ConfirmedQuantity = value;
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(nameof(ConfirmedQuantity)));
}
}
}
QuantityChanged
事件仅适用于 Quantity
属性。
为了通知 SubmittedQuantity
属性 的更改值,应该有一个 SubmittedQuantityChanged
事件:
public static event EventHandler SubmittedQuantityChanged;
public static string SubmittedQuantity
{
get => _SubmittedQuantity;
set
{
if (value != _SubmittedQuantity)
{
_SubmittedQuantity = value;
SubmittedQuantityChanged?.Invoke(null, EventArgs.Empty);
}
}
}
IMO 更好的选择是对所有静态属性使用通用的 属性 更改事件:
public static event PropertyChangedEventHandler StaticPropertyChanged;
public static string SubmittedQuantity
{
get => _SubmittedQuantity;
set
{
if (value != _SubmittedQuantity)
{
_SubmittedQuantity = value;
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(nameof(SubmittedQuantity)));
}
}
}
有关详细信息,请参阅 What's New in WPF Version 4.5 - Binding to static properties。