WPF background data trigger changes interface state-heartbeat implementation

Source: Internet
Author: User

This year to do a host computer control WPF project, do a small summary of the next time to find

Please don't spray with blood, I'm just a rookie. ___by Abalone Team

A code variable that changes the value of a variable, and the color of the circle that binds to the variable is a heartbeat effect.

In the online data trigger feeling not much, waste a lot of time, here to do a summary

1: Notice

 class   notifybase:inotifypropertychanged { public  event   PropertyChangedEventHandler propertychanged;  public  void  Onpropertychanegd ( Span style= "color: #0000ff;" >string   PropertyName) { if (propertychanged! = null   this . PropertyChanged (this ,  PropertyChangedEventArgs (PropertyName)); }}} 

class Notifymodels:notifybase     {        privatebool  heartBeat;          Public BOOL HeartBeat        {            getreturn  heartBeat;}             Set {heartBeat = value;            ONPROPERTYCHANEGD ("HeartBeat");             }}}

The first one is needless to say, the base class for notifications,

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

2: The code of the rectangle

This data triggers binding= "{Binding HeartBeat}" to bind the heartbeat of the background DataContext

Change the corresponding style change; I use the bool type, so I use the true/false; look like int can also

<ellipse x:name="Ellstatus"Verticalalignment="Center"Horizontalalignment="Center"minheight=" -"Minwidth=" -"maxheight=" -"Maxwidth=" -"> <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: Code behind the Scenes

Use a timer to change the value inside the notification

Assigned to

=
It's done.
 Public Partial classMainwindow:window {notifymodels models; PrivateSystem.Timers.Timer Timer;  PublicMainWindow () {InitializeComponent (); Models=Newnotifymodels (); Ellstatus.datacontext =  models;  Timer=NewSystem.Timers.Timer ( +); Timer. Elapsed+=NewSystem.Timers.ElapsedEventHandler (timer_elapsed); Timer. Enabled=true; }        voidTimer_elapsed (Objectsender, System.Timers.ElapsedEventArgs e) {models. HeartBeat= !models.        HeartBeat; }

WPF background data trigger changes interface state-heartbeat implementation

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.