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