WPF background data triggers interface status change-Heartbeat implementation, wpf background

Source: Internet
Author: User

WPF background data triggers interface status change-Heartbeat implementation, wpf background

This year, I am working on a host computer industrial control WPF project. I will make a small summary and try again later.

Please do not spray with blood. I'm just a cainiao. ___ by Bao

 

It is similar to this; it generally means that by changing the value of a code variable, the circle color bound to this variable is also changing, which is a heartbeat effect.

I don't feel much about triggering data on the Internet. I have spent a lot of time. Here is a summary.

1: Notification

class NotifyBase : INotifyPropertyChanged    {        public event PropertyChangedEventHandler PropertyChanged;        public void OnPropertyChanegd(string propertyName)        {            if (PropertyChanged != null)            {                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }    }

class NotifyModels:NotifyBase     {        private bool heartBeat;        public bool HeartBeat        {            get { return heartBeat; }            set { heartBeat = value;            OnPropertyChanegd("HeartBeat");            }        }            }

The first one does not need to be said. The base class of the notification,

The second is the data I need, that is, the bool type of heartbeat (it can usually detect the communication status with other communication software of the lower computer, intuitive)

2: rectangular code

This data triggerBinding = "{Binding HeartBeat }"Bind the background DataContext heartbeat

Change the corresponding style. I use the bool type, so the True/False type is used. It can be seen similar to the int type.

 <Ellipse x:Name="ellStatus"  VerticalAlignment="Center" HorizontalAlignment="Center"     MinHeight="50" MinWidth="50" MaxHeight="50" MaxWidth="50"  >            <Ellipse.Style >                <Style TargetType="{x:Type Ellipse}">                    <Style.Triggers>                        <DataTrigger Binding="{Binding  HeartBeat}" Value="True">                            <Setter Property="Shape.Fill" Value="Red"/>                        </DataTrigger>                        <DataTrigger Binding="{Binding  HeartBeat}" Value="False">                            <Setter Property="Shape.Fill" Value="White"/>                        </DataTrigger>                    </Style.Triggers>                </Style>            </Ellipse.Style>        </Ellipse>

 

 

3: Background code

Use a timer to change the value in the notification.

Assigned

EllStatus. DataContext=Models;
It's done.
public partial class MainWindow : Window    {        NotifyModels models;        private System.Timers.Timer timer;        public MainWindow()        {            InitializeComponent();            models = new NotifyModels();            ellStatus.DataContext = models;             timer = new System.Timers.Timer(1000);            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);            timer.Enabled = true;        }        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)        {            models.HeartBeat = !models.HeartBeat;        }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.