WPF绑定.NET对象(二)Visifire的数据绑定

Visifire在visifire_v3.6.8之前授权都是GPL v3的,而且也算是比较强大的chart插件。

之前有写过WPF绑定.NET对象属性简单实例

这里实战,因为项目用到,顺便这里做个笔记。

Visifire数据绑定的方式:<Window x:Class="DataBindingInWPFVisifireChart.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="400" Width="580"
        xmlns:vc="clr-namespace:Visifire.Charts;assembly=WPFVisifire.Charts"
        >
    <Grid>
        <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center"  >

            <vc:Chart Name="MyChart" Width="500" Height="300" Padding="10,10" Margin="10,0" AnimatedUpdate="True" Theme="Theme5">

                <vc:Chart.Titles>
                    <vc:Title Text="Visifire DataBinding"></vc:Title>
                </vc:Chart.Titles>

                <vc:Chart.Series>
                    <vc:DataSeries RenderAs="StackedArea" >
                        <vc:DataSeries.DataMappings>
                            <vc:DataMapping MemberName="AxisXLabel" Path="Label"></vc:DataMapping>
                            <vc:DataMapping MemberName="YValue" Path="YValue"></vc:DataMapping>
                        </vc:DataSeries.DataMappings>
                    </vc:DataSeries>
                </vc:Chart.Series>
            </vc:Chart>

            <Button Margin="10" Click="Button_Click" Width="80">add</Button>
       <Button Margin="10" Click="Button1_Click" Width="80">remove</Button>
</StackPanel> </Grid> </Window>

要绑定的对象设计:

namespace DataBindingInWPFVisifireChart
{
    public class ValuesCollection : ObservableCollection<Value> { };

    public class Value : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        Double _yValue;
        String _label;

        public String Label
        {
            get
            {
                return _label;
            }
            set
            {
                _label = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Label"));
            }
        }

        public Double YValue
        {
            get
            {
                return _yValue;
            }
            set
            {
                _yValue = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("YValue"));
            }
        }
    }
}

实现 INotifyPropertyChanged 接口和为绑定类型的每个属性提供更改事件,ok

测试:

        public MainWindow()
        {
            InitializeComponent();
            values.Add(new Value() { Label = "Sony", YValue = 50 });
            values.Add(new Value() { Label = "Dell", YValue = 35 });
            values.Add(new Value() { Label = "HP", YValue = 27 });
            values.Add(new Value() { Label = "HCL", YValue = 17 });
            values.Add(new Value() { Label = "Toshiba", YValue = 16 });

            MyChart.Series[0].DataSource = values;
        }

        ObservableCollection<Value> values = new ObservableCollection<Value>();

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            values.Add(new Value() { Label = "add", YValue = 33.3 });
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            if (values.Count > 0)
            {
                values.RemoveAt(values.Count - 1);
            }
        }    

 

 

 

posted @ 2013-04-03 16:07  xiepeixing  阅读(2464)  评论(1编辑  收藏  举报