当属性值更改时向 wpf 控制器添加事件

发布于 2024-12-21 09:45:26 字数 740 浏览 1 评论 0原文

我有一个属性

public sealed partial class Computer
{
    private bool _online;
    public bool Online
    {
        get { return _online; }
        set
        {
            _online = value;
            RaiseProperty("Online");
        }
    }
 }

,它引发 INotifyPropertyChanged 类型的事件

public sealed partial class Computer : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

    private void RaiseProperty(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 }

我的问题是,如何添加一个附加事件,告诉 TabControl 在这种情况下每次在线属性更改时运行特定方法?

I have a property

public sealed partial class Computer
{
    private bool _online;
    public bool Online
    {
        get { return _online; }
        set
        {
            _online = value;
            RaiseProperty("Online");
        }
    }
 }

Which raises an event of type INotifyPropertyChanged

public sealed partial class Computer : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

    private void RaiseProperty(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 }

My question is, how can I add an additional event telling in this case an TabControl to run a specific method each time the Online Property changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最冷一天 2024-12-28 09:45:26

您需要向 PropertyChanged 事件注册一个方法

MyComputer.PropertyChanged += Computer_PropertyChanged;

void Computer_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Online")
    {
        // Do Work
    }
}

You need to register a method to the PropertyChanged event

MyComputer.PropertyChanged += Computer_PropertyChanged;

void Computer_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Online")
    {
        // Do Work
    }
}
时光礼记 2024-12-28 09:45:26
public sealed partial class Computer
{
    // This event is fired every time when Online is changed
    public event EventHandler OnlineChanged;

    private bool _online;
    public bool Online
    {
        get { return _online; }
        set
        {
            // Exit if online value isn't changed
            if (_online == value) return;

            _online = value;
            RaiseProperty("Online");

            // Raise additional event only if there are any subscribers
            if (OnlineChanged != null)
                OnlineChanged(this, null);
        }
    }
}

您可以像这样使用此事件:

Computer MyComputer = new MyComputer();
MyComputer.OnlineChanged += MyComputer_OnlineChanged;

void MyComputer_OnlineChanged(object sender, EventArgs e)
{
    Computer c = (Computer)c;
    MessageBox.Show("New value is " + c.Online.ToString());
}
public sealed partial class Computer
{
    // This event is fired every time when Online is changed
    public event EventHandler OnlineChanged;

    private bool _online;
    public bool Online
    {
        get { return _online; }
        set
        {
            // Exit if online value isn't changed
            if (_online == value) return;

            _online = value;
            RaiseProperty("Online");

            // Raise additional event only if there are any subscribers
            if (OnlineChanged != null)
                OnlineChanged(this, null);
        }
    }
}

You can use this event like:

Computer MyComputer = new MyComputer();
MyComputer.OnlineChanged += MyComputer_OnlineChanged;

void MyComputer_OnlineChanged(object sender, EventArgs e)
{
    Computer c = (Computer)c;
    MessageBox.Show("New value is " + c.Online.ToString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文