WPF页面刷新

WPF页面刷新:

在WPF项目中,类的属性变了,但界面值没有变,且我们也没有对类的属性做PropertyChanged的set处理.

如何实现呢?

首先我们需要实现一个INotifyPropertyChanged。

并实现一个方法:

public void OnPropertyChanged(string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

在类修改后,使用OnPropertyChanged(null);即可。

我的定义如下:

public class BaseViewModel: INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        protected bool SetProperty<T>(ref T properValue, T newValue, string properName = null)
        {
            if (object.Equals(properValue, newValue))
                return false;
            properValue = newValue;
            OnPropertyChanged(properName);
            return true;
        }
        public void OnPropertyChanged(string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

以上做了基类,数据类继承于它。

public class MyDataModel : BaseViewModel

使用时:

MyDataModel.value="Test";

MyDataModel.OnPropertyChanged(null);

即可.

 

转载于:https://www.cnblogs.com/youmeetmehere/p/10978526.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,需要实现以下几个功能: 1. 在WPF中打开A页面,并在一定时间间隔内刷新A页面。 2. 点击按钮弹出页面B,并在一定时间间隔内刷新弹出页面B。 3. 关闭弹出页面B后,重新开始刷新A页面。 4. 关闭A页面时,A和B页面刷新都要停止。 以下是实现的大致思路: 1. 在A页面的构造函数中,启动一个定时器,定时刷新A页面。可以使用System.Windows.Threading.DispatcherTimer类实现定时器功能。 ```csharp public partial class APage : Page { private DispatcherTimer timer; public APage() { InitializeComponent(); // 初始化定时器 timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(5); timer.Tick += Timer_Tick; timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { // 刷新A页面 // ... } // ... } ``` 2. 在A页面中添加一个按钮,点击按钮时弹出页面B。可以使用System.Windows.Window类实现窗口弹出功能。 ```csharp public partial class APage : Page { // ... private void Button_Click(object sender, RoutedEventArgs e) { // 弹出页面B BWindow bWindow = new BWindow(); bWindow.Show(); } // ... } ``` 3. 在B页面的构造函数中,启动一个定时器,定时刷新B页面。在B页面关闭时,停止定时器并重新开始刷新A页面。 ```csharp public partial class BWindow : Window { private DispatcherTimer timer; public BWindow() { InitializeComponent(); // 初始化定时器 timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(5); timer.Tick += Timer_Tick; timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { // 刷新B页面 // ... } private void Window_Closed(object sender, EventArgs e) { // 停止定时器并重新开始刷新A页面 timer.Stop(); APage aPage = new APage(); aPage.StartTimer(); } // ... } ``` 4. 在A页面的关闭事件中,停止定时器并关闭B页面。可以使用System.Windows.Application类的MainWindow属性获取当前应用程序的主窗口,从而关闭B页面。 ```csharp public partial class APage : Page { // ... private void Page_Unloaded(object sender, RoutedEventArgs e) { // 停止定时器并关闭B页面 timer.Stop(); Window mainWindow = Application.Current.MainWindow; foreach (Window window in mainWindow.OwnedWindows) { window.Close(); } } // ... } ``` 以上代码仅为参考,具体实现还需要根据您的实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值