我目前在WPF 4.6.1中遇到双向绑定问题。它只是不起作用,我用这个例子。 https://www.c-sharpcorner.com/UploadFile/mahesh/binding-static-properties-in-wpf-4-5/
我的项目有两个窗口(在第一个窗口按下TextBlock时打开的数字小键盘和显示数据的窗体)。它还有一个静态类,用于保存在虚拟键盘上输入的信息。
这是打开键盘的TextBox
<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"/>
这是我的静态类,包含信息。
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为空。
当静态类的值发生变化时,有人能告诉我为什么它不会更新文本块吗?
由于
按照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"/>
静态类
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)));
}
}
}
答案 0 :(得分:2)
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。