gpt4 book ai didi

wpf - 如何在 WPF/XAML 中正确使用 INotifyPropertyChanged

转载 作者:行者123 更新时间:2023-12-04 06:48:06 29 4
gpt4 key购买 nike

我有一个要绑定(bind)到控件的自定义对象。在那个自定义对象上,我实现了 INotifyPropertyChanged 接口(interface)。我已成功绑定(bind)到我的对象和该对象上的属性。

我不知道如何从那里去。我已经为此工作了 2 天,但仍然无法正常工作。

我的假设是,当我更改绑定(bind)到控件的属性时,该属性中设置的值将显示在控件中。但是,无论我如何更改属性,UI 都不会更新到超出其初始值。

我以这种方式实现了 INotifyPropertyChanged:
A base class which implements INotifyPropertyChanged

所以我的基类是这样的:

[Serializable]
public abstract class BindableObject : INotifyPropertyChanged
{
#region Data

private static readonly Dictionary<string, PropertyChangedEventArgs> eventArgCache;
private const string ERROR_MSG = "{0} is not a public property of {1}";

#endregion // Data

#region Constructors

static BindableObject()
{
eventArgCache = new Dictionary<string, PropertyChangedEventArgs>();
}

protected BindableObject()
{
}

#endregion // Constructors

#region Public Members

/// <summary>
/// Raised when a public property of this object is set.
/// </summary>
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Returns an instance of PropertyChangedEventArgs for
/// the specified property name.
/// </summary>
/// <param name="propertyName">
/// The name of the property to create event args for.
/// </param>
public static PropertyChangedEventArgs
GetPropertyChangedEventArgs(string propertyName)
{
if (String.IsNullOrEmpty(propertyName))
throw new ArgumentException(
"propertyName cannot be null or empty.");

PropertyChangedEventArgs args;

// Get the event args from the cache, creating them
// and adding to the cache if necessary.
lock (typeof(BindableObject))
{
bool isCached = eventArgCache.ContainsKey(propertyName);
if (!isCached)
{
eventArgCache.Add(
propertyName,
new PropertyChangedEventArgs(propertyName));
}

args = eventArgCache[propertyName];
}

return args;
}

#endregion // Public Members

#region Protected Members

/// <summary>
/// Derived classes can override this method to
/// execute logic after a property is set. The
/// base implementation does nothing.
/// </summary>
/// <param name="propertyName">
/// The property which was changed.
/// </param>
protected virtual void AfterPropertyChanged(string propertyName)
{
}

/// <summary>
/// Attempts to raise the PropertyChanged event, and
/// invokes the virtual AfterPropertyChanged method,
/// regardless of whether the event was raised or not.
/// </summary>
/// <param name="propertyName">
/// The property which was changed.
/// </param>
protected void RaisePropertyChanged(string propertyName)
{
this.VerifyProperty(propertyName);

PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
// Get the cached event args.
PropertyChangedEventArgs args =
GetPropertyChangedEventArgs(propertyName);

// Raise the PropertyChanged event.
handler(this, args);
}

this.AfterPropertyChanged(propertyName);
}

#endregion // Protected Members

#region Private Helpers

[Conditional("DEBUG")]
private void VerifyProperty(string propertyName)
{
Type type = this.GetType();

// Look for a public property with the specified name.
PropertyInfo propInfo = type.GetProperty(propertyName);

if (propInfo == null)
{
// The property could not be found,
// so alert the developer of the problem.

string msg = string.Format(
ERROR_MSG,
propertyName,
type.FullName);

Debug.Fail(msg);
}
}

#endregion // Private Helpers
}

我从上面的那个类继承,在我的派生类中,我在我的属性(property)上这样做:
    public virtual string Name
{
get
{
return m_strName;
}
set
{
m_strName = value;
RaisePropertyChanged("Name");
}
}

我的 XAML 看起来像这样(缩写版):
<Window x:Class="PSSPECApplication.Windows.Project"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
DataContext="{Binding SizingProject, RelativeSource={RelativeSource Self}}">
<StackPanel VerticalAlignment="Center">
<TextBox Name="txtProjectName" Text="{Binding Name}" />
</StackPanel>

您可以看到窗口的数据上下文是一个名为 SizingProject 的属性。 SizingProject 属于派生类型(从 BindableObject 派生),其中包含 Name 属性并引发 PropertyChanged 事件处理程序。

在我的窗口的构造函数中,我填充了 SizingProject 并设置了它的 Name 属性。

为了测试这一点,我在窗口上还有一个按钮,它触发一个事件,将 Name 属性设置为不同于原来的值。但是,当更改 name 属性时,什么也没有发生。我已经追溯到 BindableObject 并且 PropertyChanged 事件始终设置为 null,因此没有设置和运行任何处理程序。为什么是这样?

我想通过实现 INotifyPropertyChanged 并在绑定(bind)中使用该类型强制 WPF 自动设置该事件处理程序,然后发生正确的行为?对我来说,我从未见过这种行为。

我弄清楚了这个问题。我需要做的是为我的属性 SizingProject 创建一个 DependencyProperty。在我这样做之后,一切正常。
        public static readonly DependencyProperty SizingProjectProperty =
DependencyProperty.Register("SizingProject", typeof(Sizing.Project), typeof(Project), new UIPropertyMetadata());

public Sizing.Project SizingProject
{
get
{
return (Sizing.Project)GetValue(Project.SizingProjectProperty);
}
set
{
SetValue(Project.SizingProjectProperty, value);
}
}

最佳答案

在我的机器上工作。虽然,缓存有点古怪。我要么为每种类型创建静态只读版本,要么在需要之前忘记缓存(过早的优化和所有)。

我创建了一个示例项目。主窗口如下所示:

<Window x:Class="INPCTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:this="clr-namespace:INPCTest"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<this:MyObject />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock
Text="{Binding MyOutProperty}" />
<TextBox
Grid.Row="1"
Text="{Binding MyInProperty, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>

我正在绑定(bind)到我在 xaml 中创建的 MyObject 的一个实例(如果这对您来说不熟悉,您可以在代码隐藏中执行此操作)。

这是 MyObject 的代码:
class MyObject : BindableObject
{
private string _in;
private string _out;
public string MyOutProperty
{
get { return _out; }
set { _out = value; this.RaisePropertyChanged("MyOutProperty"); }
}
public string MyInProperty
{
get { return _in; }
set
{
_in = value;
MyOutProperty = "The textbox below says: \"" + value + "\"";
this.RaisePropertyChanged("MyInProperty");
}
}
}

这一切如何协同工作是:
  • 窗口创建
  • 的实例我的对象 被实例化并设置为 Window.DataContext
  • TextBlock 绑定(bind)到 MyOutProperty
  • TextBox 绑定(bind)到 MyInProperty
  • 用户在文本框中输入“X”
  • MyInProperty 设置为“X”
  • MyOutProperty 设置为“下面的文本框显示:“X””
  • MyOutProperty 的 Set 方法调用 提高属性已更改 传入“MyOutProperty”
  • TextBlock 按预期更新。

  • 我怀疑您的问题不在于您的基类,而在于您的子类的实现或绑定(bind)中。

    为了帮助调试您的绑定(bind), follow the information at this link为详细绑定(bind)跟踪输出配置 Visual Studio(如果您已配置,则在“输出”窗口或“立即”窗口中结束)。

    关于wpf - 如何在 WPF/XAML 中正确使用 INotifyPropertyChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3505716/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com