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; }