Data dynamics for a component of a WPF histogram that supports dynamic database updates

Source: Internet
Author: User
Tags visibility

WPF histogram (Support for database dynamic update)

In this article we describe how to wrap a histogram into a component, to open the properties of the component to the outside of the component's internal properties, and to dynamically update the data in a non-polling way.

Non-polling update data feel the introduction of not enough detail please see this article

WPF non-polling update database changes SqlDependency (database modification foreground automatic update)

The histogram component, however, says that so many components are available but there is no bar chart. Today, let's talk about the bar chart.

About the histogram what I use here is also downloaded on the Internet, we talk about the data dynamics of the component today, do not pay attention to the specific histogram how to draw, but let when the properties of the component changes, the bar chart will also change.

There is seldom such information on the Internet, we should change it ourselves today.

First, how do we bind the properties of a component to the properties of a bar chart?

In the previous article we knew that the histogram was a user control, and we went to register the dependency property for this user control.

 Public StaticDependencyProperty Titleproperty = Dependencyproperty.register ("Title",typeof(string),typeof(Barchart)); Public stringTitle {Get{return(string) GetValue (Titleproperty); }            Set{SetValue (titleproperty, value);} }

In this code, a property and a dependency property are registered, and the title is a caption for the bar chart.

Next, bind the title to the title of the bar chart.

New  This New PropertyPath ("Title")};            Bindingoperations.setbinding (this. Txttoptitle, Textblock.textproperty, Titlebind);

This.txttoptitle is a TextBlock control on a user control that is bound in this way.

Now just bind the property to the user control so how does the data come about?

Where the component gets the data, add

New  This New PropertyPath ("Title")};                Bindingoperations.setbinding (this. Chart2, Barchart.titleproperty, Titlebind);


This.chart2 is a histogram user control, Barchart.titleproperty is our registered dependency property

That way, as long as the external component property changes, the inner bar chart changes.

The data of the histogram becomes the same as the motion

/// <summary>        ///creates the chart based on the DataSource. /// </summary>         Public voidGenerate () {Try{ Left=0; Legends.                Clear ();                ChartArea.Children.Clear (); //Setup chart elements.Addchartcontrolstochart (); //Setup chart area.Setupchartarea (); //'ll is made more generic in the next versions.DataTable dt = (DataSource asDataSet). tables[0]; if(NULL!=DT) {                    //if no data found draw empty chart.                    if(dt. Rows.Count = =0) {Drawemptychart (); return; }                    //Hide the nodata found text.Txtnodata.visibility =Visibility.hidden; //Get the Max Y-value. this was used to calculate the scale and y-axis.MaxData =getmax (DT); //Prepare the chart for rendering. Does some basic setup.preparechartforrendering (); //Get the total bar count.                    intBarcount =dt.                    Rows.Count; //If more than 1 the Value field, then the is a group chart.                    BOOLIsseries = valuefield.count >1; //no legends added yet.                    BOOLlegendadded =false;//no legends yet added. //For each row in the datasource                    foreach(DataRow rowinchdt. Rows) {//Draw X-axis label based on DataRow.Drawxaxislabel (row); //Set the BarWidth. This was required to adjust the size of based on available No. of//bars.setbarwidth (Barcount); //For each row, the current series are initialized to 0 to indicate start of series.                        intCurrentseries =0; //for each value in the DataRow, draw the bar.                        foreach(stringValfieldinchValuefield) {                            if(NULL==Valfield)Continue; if(!row. Table.Columns.Contains (Valfield))Continue; //Draw bar for each value.Drawbar (Isseries, legendadded, Row,refcurrentseries, Valfield); } legendadded=true; //Set up location for next bar in series.                        if(isseries) left= left +Spacebetweenbars; }                    //Reset the ChartArea to accomdodate all the chart elements.                    if(left + barwidth) >chartarea.width) Chartarea.width= left +BarWidth; //Draw the x-axis.Drawxaxis (); //Draw the y-axis.Drawyaxis (); //Draw the legend.Drawlegend (); }            }            Catch(Exception ex) {//todo:finalize exception handling strategy.MessageBox.Show (ex.            Message); }        }

This is the code of the histogram, to achieve the effect of dynamic changes, but also need to add the following code:

PrivateDataSet data; Private BOOLShowbar; protected Override voidonpropertychanged (DependencyPropertyChangedEventArgs e) {Base.            OnPropertyChanged (e); //DataTable dt = data. Tables[0];            if(Data! = DataSource | | Showbar! =Showvalueonbar)            Generate (); Data=DataSource; Showbar=Showvalueonbar; }        protected Override voidOncontentchanged (ObjectOldcontent,Objectnewcontent) {            Base.            Oncontentchanged (Oldcontent, newcontent); if(Data! = DataSource | | Showbar! =Showvalueonbar)            Generate (); Data=DataSource; Showbar=Showvalueonbar; }

Re-plot the property and content when it changes, to achieve the effect of data changes.

Need all the source code, please dabigatran.

WPF, AE technology Group: 94234450
We can't guarantee to solve any problems, but will never let you face it alone!

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.