WPF Gauge中的动态值

时间:2017-09-25 18:36:51

标签: c# wpf gauge livecharts

我是WPF和C#的新手,但是我很快就感谢这个社区。我试图将实时图表中的Angular Gauge实现到我的项目中。只使用一个绑定值,其余的值就像这个例子中那样固定,它很有用:(https://lvcharts.net/App/examples/v1/wpf/Angular%20Gauge)。

有人可以帮助我调整我的代码以使其动态化吗?而不是在 FromValue中使用固定值: ToValue:我想将它们绑定到我的数据库中的值,或者根据我的目标计算它们。所有建议都非常受欢迎。

这是我到目前为止所做的工作:

XAML:

<lvc:AngularGauge  Value="{Binding Value}" FromValue="0" Grid.Column="1" ToValue="250" 
      LabelsStep="50" TicksStep="25" Wedge="300"
      TicksForeground="White" Foreground="WhiteSmoke" 
      FontWeight="Bold" FontSize="16"
      SectionsInnerRadius=".6" Width="310">
    <lvc:AngularGauge.Sections>
        <lvc:AngularSection FromValue="0" ToValue="62.5" Fill="#dd5143"/>
        <lvc:AngularSection FromValue="62.5" ToValue="125" Fill="#e68523"/>
        <lvc:AngularSection FromValue="125" ToValue="187.5" Fill="#edb220"/>
        <lvc:AngularSection FromValue="175" ToValue="250" Fill="#7cb82f"/>
    </lvc:AngularGauge.Sections>
</lvc:AngularGauge>

C#:

namespace Car_App

    public partial class MainWindow : MetroWindow
    {
        private double _value;

        public MainWindow()
        {
            InitializeComponent();

            string connectionString = "datasource=xx.xx.xxx.xxx;port=xxxx;username=xxxx;password=xxx";
            string sMonth = DateTime.Now.ToString("MM");
            string sYear = DateTime.Now.ToString("yyyy");

            MySqlConnection connection = new MySqlConnection(connectionString);

            MySqlCommand cmd = new MySqlCommand("Select * from Table.MyTable where MONTH(Date) = @sMonth AND YEAR(Date) = @sYear", connection);
            MySqlCommand sCarCT = new MySqlCommand("Select TotalCarCount from Table.CarTotals where sMonth = @sMonth", connection);
            MySqlCommand target = new MySqlCommand("Select Target from Table.Targets where Location = 'xxxxx'", connection);

            try
            {
            connection.Open();
                cmd.Parameters.Add(new MySqlParameter("sMonth", sMonth));
                cmd.Parameters.Add(new MySqlParameter("sYear", sYear));
                sCarCT.Parameters.Add(new MySqlParameter("sMonth", sMonth));

                DataTable dt = new DataTable();
                dt.Load(cmd.ExecuteReader());
                dtGrid.DataContext = dt;

                Value = double.Parse(sCarCT.ExecuteScalar().ToString());
                DataContext = this;

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            connection.Close();
        }

        public double Value
        {
            get { return _value; }
            set
            {
                _value = value;
            }
        }

    }

我想要实现但不起作用(我知道它没有工作机会,但我希望有人可以帮我正确地重写它):

XAML:

<lvc:AngularGauge  Value="{Binding Value}" FromValue="0" Grid.Column="1" ToValue="{Binding ValueT}"
      LabelsStep="50" TicksStep="25" Wedge="300"
      TicksForeground="White" Foreground="WhiteSmoke" 
      FontWeight="Bold" FontSize="16"
      SectionsInnerRadius=".6" Width="310">
    <lvc:AngularGauge.Sections>
        <lvc:AngularSection FromValue="0" ToValue="{Binding Value25}" Fill="#dd5143"/>
        <lvc:AngularSection FromValue="{Binding Value25}" ToValue="{Binding Value50}" Fill="#e68523"/>
        <lvc:AngularSection FromValue="{Binding Value50}" ToValue="{Binding Value75}" Fill="#edb220"/>
        <lvc:AngularSection FromValue="{Binding Value75}" ToValue="{Binding ValueT}" Fill="#7cb82f"/>
    </lvc:AngularGauge.Sections>
</lvc:AngularGauge>

C#:

namespace Car_App

    public partial class MainWindow : MetroWindow
    {
        private double _value;

        public MainWindow()
        {
            InitializeComponent();

            string connectionString = "datasource=xx.xx.xxx.xxx;port=xxxx;username=xxxx;password=xxx";
            string sMonth = DateTime.Now.ToString("MM");
            string sYear = DateTime.Now.ToString("yyyy");

            MySqlConnection connection = new MySqlConnection(connectionString);

            MySqlCommand cmd = new MySqlCommand("Select * from Table.Car where MONTH(Date) = @sMonth AND YEAR(Date) = @sYear", connection);
            MySqlCommand sCarCT = new MySqlCommand("Select TotalCarCount from Table.CarTotals where sMonth = @sMonth", connection);
            MySqlCommand target = new MySqlCommand("Select Target from Table.Targets where Location = 'xxxxx'", connection);

            try
            {
            connection.Open();
                cmd.Parameters.Add(new MySqlParameter("sMonth", sMonth));
                cmd.Parameters.Add(new MySqlParameter("sYear", sYear));
                sCR.Parameters.Add(new MySqlParameter("sMonth", sMonth));


                DataTable dt = new DataTable();
                dt.Load(cmd.ExecuteReader());
                dtGrid.DataContext = dt;


                Value = double.Parse(sCarCT.ExecuteScalar().ToString());

                ValueT = double.Parse(target.ExecuteScalar().ToString());

                Value75 = ValueT - ValueT*25%;

                Value50 = ValueT - ValueT*50%;

                Value25 = ValueT - ValueT*75%;    

                DataContext = this.Value, this.ValuT, this.Value25, this.Value50, this.Value75;


            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            connection.Close();
        }


        public double Value
        {
            get { return _value; }
            set
            {
                _value = value;
            }
        }

        public double ValueT
        {
            get { return _value; }
            set
            {
                _value = value;

            }
        }

        public double Value75
        {
            get { return _value; }
            set
            {
                _value = value;
            }
        }

        public double Value50
        {
            get { return _value; }
            set
            {
                _value = value;
            }
        }

        public double Value25
        {
            get { return _value; }
            set
            {
                _value = value;
            }
        }

    }

1 个答案:

答案 0 :(得分:2)

我不熟悉这个特定的库,但查看source code表明FromValue和ToValue属性是依赖属性,因此可以将它们绑定到一个值。看起来您可能只是将您的值属性实现为DependencyProperty

尝试实现与此类似的属性:

public static readonly DependencyProperty Value25Property = DependencyProperty.Register(
    "Value25", 
    typeof(double), 
    typeof(MainWindow));

public double Value25
{
    get { return (double) GetValue(Value25Property); }
    set { SetValue(Value25Property, value); }
}