WPF柱状图(支持数据库动态更新)之组件的数据动态化
在这片文章中我们介绍了如何将柱状图包装成一个组件,将这个组件的属性对外开放和组件的外部属性根内部属性绑定以及非轮询动态更新数据的方式。
非轮询更新数据感觉介绍的不够详细的请看这篇文章
WPF非轮询方式更新数据库变化SqlDependency(数据库修改前台自动更新)
然而柱状图组件讲了这么多 组件是有了 但是没有柱状图。今天去讲一下柱状图。
关于柱状图呢 我这里用的也是网上下载下来的,我们今天讲组件的数据动态化,不关注具体的柱状图怎么画,而是让当组件的属性发生更改的时候,柱状图也会变化。
网上一般很少有这种资料,今天我们自己去改。
首先我们怎样将组件的属性和柱状图的属性去绑定呢?
在之前的文章中我们知道柱状图是一个用户控件,我们去为这个用户控件注册依赖属性。
public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(BarChart));
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
在这段代码中,注册了一个属性和依赖属性,title为柱状图的一个标题。
接下来将这个Title绑定到柱状图的Title上。
Binding titleBind = new Binding { Source = this, Path = new PropertyPath("Title") };
BindingOperations.SetBinding(this.txtTopTitle, TextBlock.TextProperty, titleBind);
this.txtTopTitle是用户控件上的一个textblock控件,用这种方式绑定即可。
现在只是将属性和用户控件绑定了 那么数据怎么来的呢?
在组件获取数据的地方 加上
Binding titleBind = new Binding { Source = this, Path = new PropertyPath("Title") };
BindingOperations.SetBinding(this.chart2, BarChart.TitleProperty, titleBind);
this.chart2是柱状图用户控件,BarChart.TitleProperty是我们注册的依赖属性
这样一来只要外部的组件属性发生改变 内部的柱状图也会发生改变
柱状图的数据变得动也是同理
/// <summary>
/// Creates the chart based on the datasource.
/// </summary>
public void Generate()
{
try
{
left = ;
legends.Clear();
chartArea.Children.Clear(); // Setup chart elements.
AddChartControlsToChart(); // Setup chart area.
SetUpChartArea(); // Will be made more generic in the next versions.
DataTable dt = (DataSource as DataSet).Tables[]; if (null != dt)
{
// if no data found draw empty chart.
if (dt.Rows.Count == )
{
DrawEmptyChart();
return;
} // Hide the nodata found text.
txtNoData.Visibility = Visibility.Hidden; // Get the max y-value. This is 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.
int barCount = dt.Rows.Count;
// If more than 1 value field, then this is a group chart.
bool isSeries = ValueField.Count > ; // no legends added yet.
bool legendAdded = false; // no legends yet added. // For each row in the datasource
foreach (DataRow row in dt.Rows)
{
// Draw x-axis label based on datarow.
DrawXAxisLabel(row); // Set the barwidth. This is required to adjust the size based on available no. of
// bars.
SetBarWidth(barCount); // For each row the current series is initialized to 0 to indicate start of series.
int currentSeries = ; // For each value in the datarow, draw the bar.
foreach (string valField in ValueField)
{
if (null == valField)
continue; if (!row.Table.Columns.Contains(valField))
continue; // Draw bar for each value.
DrawBar(isSeries, legendAdded, row, ref currentSeries, 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);
}
}
这是画柱状图的代码,要达到动态变动的效果,还需要加入如下代码:
private DataSet data;
private bool showBar;
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
//DataTable dt = data.Tables[0]; if (data != DataSource || showBar != ShowValueOnBar)
Generate();
data = DataSource;
showBar = ShowValueOnBar;
}
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
if (data != DataSource || showBar != ShowValueOnBar)
Generate();
data = DataSource;
showBar = ShowValueOnBar;
}
在属性和内容发生变动的时候重新绘图,达到数据变动的效果。
需要全部源码的请加群。
WPF、AE技术交流群:94234450
点击加入QQ群:
我们不能保证解决任何问题,但绝不会让你独自去面对!
WPF柱状图(支持数据库动态更新)之组件的数据动态化的更多相关文章
- WPF柱状图(支持数据库动态更新)
之前我们讲到wpf组件基类以及组件开发,现在我们围绕之前的内容去开发一个组件. 效果图请加群查看,在群共享里面. 做出这个呢 是比较繁琐的. 首先要使用我们的基类 继承基类的模板自动生成如下几个文件 ...
- bootstrap-multiselect.js如何动态更新select里的数据
在使用jQuery的bootstrap-multiselect插件时可能会遇到一个问题 就是想要动态的去更新select里的数据 比如我们要使一个id=select的选择框实现多选 那么先用ajax获 ...
- Android AlertDialog 动态更新里面的ListView数据
1:和ListView的数据跟新是基本一样的. 2:Activity代码示例 public class MainActivity extends AppCompatActivity { AlertDi ...
- WPF非轮询方式更新数据库变化SqlDependency(数据库修改前台自动更新)
上一章节我们讲到wpf的柱状图组件,它包含了非轮询方式更新数据库变化SqlDependency的内容,但是没有详细解释,现在给大家一个比较简单的例子来说明这部分内容. 上一章节: WPF柱状图(支持数 ...
- Vue--父组件传数据给子组件,子组件生命周期过程拿到数据的情况
需求: 在子组件渲染之前,我要修改数据的某个字段 结果是 组件在beforeUpdate,updated 的状态才能拿到父组件的数据 那么证明,我根本无法在beforeUpdate,updated两个 ...
- 如果把父组件的数据实时的传递到子组件:用watch
1.在子组件使用watch来监听传递给子组件的数据,然后更新子组件的数据. 2.watch和computed结合使用效果非常好. 参考链接:https://blog.csdn.net/zhouweix ...
- highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度
highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度 作者:highcharts | 时间:2014-6-11 14:07:05 | [小 大] | ...
- 【Docker】数据库动态授权组件在Kubernetes集群下的测试过程记录
背景 我们都知道出于安全性考虑,生产环境的权限一般都是要做最小化控制,尤其是数据库的操作授权,更是重中之重. 博主所在公司使用的是Kubernetes(k8s)进行的集群容器管理,因为容器发布时的IP ...
- WPF中动态更新TextBlock文字中的超链接,文本
1.------------------------------------------------------------------------- 修改超链接的文本文字: <TextBloc ...
随机推荐
- int指令理解
以下是王爽老师的<汇编语言>中第十五章中的一段程序代码,其功能是增加9号中断的功能,当按下Esc键时屏幕中显示的字母改变颜色 assume cs:codesg,ss:stack,ds:da ...
- Layout Resource官方教程(1)简介
Layout Resource SEE ALSO Layouts A layout resource defines the architecture for the UI in an Activit ...
- net中System.Security.Cryptography 命名空间 下的加密算法
.net中System.Security.Cryptography命名空间 在.NETFramework出现之前,如果我们需要进行加密的话,我们只有各种较底层的技术可以选择,如 Microsoft C ...
- wzplayer for ios 针对(mms)优化版本V1.0
wzplayer for ios针对mms优化版本发布. 1.支持mms,http,rtmp,rtsp等协议 2.支持全格式 下载地址:http://www.coolradio.cn/WzPlayer ...
- Getting Started Tutorial
Getting Started Tutorial The topics contained in this section are intended to give you quick exposur ...
- [Hadoop源码解读](五)MapReduce篇之Writable相关类
前面讲了InputFormat,就顺便讲一下Writable的东西吧,本来应当是放在HDFS中的. 当要在进程间传递对象或持久化对象的时候,就需要序列化对象成字节流,反之当要将接收到或从磁盘读取的字节 ...
- sql 不同server間寫入數據
select * from sys.servers sp_dropserver @server =N'' sp_dropserver '' ,'droplogins' EXEC master.dbo. ...
- OpenSSH ‘mm_newkeys_from_blob’函数权限许可和访问控制漏洞
漏洞名称: OpenSSH ‘mm_newkeys_from_blob’函数权限许可和访问控制漏洞 CNNVD编号: CNNVD-201311-117 发布时间: 2013-11-12 更新时间: 2 ...
- word 中Sentences、Paragraph等含义和用法
word 中有Words,Characters,Sentences.Paragraph,Sections 具体含义如下表达式 含义 返回的对象 Words(index) ...
- HDU 1518
思路:从第一个数开始搜索,将其和与边长比对,相等则计数+1,计数达到3的时候说明可以组成,因为剩下那条必与边长相等,搜索过程注意剪枝,若某个数已被加入边长则不能重复计算,应将其标记,另外应在每一层递归 ...