2017-09-25 19 views
-1

私はWPFとC#で新しいですが、私はこのコミュニティのおかげで非常に迅速に取り上げています。私はライブチャートから私のプロジェクトにAngular Gaugeを実装しようとしました。これは、1つのバインドされた値と、この例のように固定された残りの値:(https://lvcharts.net/App/examples/v1/wpf/Angular%20Gauge)でうまくいきます。WPFゲージの動的値

動的にするために自分のコードを適応させるのに助けてもらえますか? の固定値の代わりに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

あなたの質問は特に良い[MCVE]を欠いている、非常に広いです。しかし、 'MainWindow'クラスで' INotifyPropertyChanged'を実装したようには見えません。それらのプロパティのために別々のビューモデルクラスを作成する方が良いでしょうが、バインディングが初期化された後に設定された値をバインディングに反映させるには、プロパティが属するクラスがINotifyPropertyChangedを実装する必要があります。 –

+0

こんにちはピーター!申し訳ありませんが、私はガイドラインに従うことができませんでした。最初の例は、検証可能で動作している場合です。それで、もう1つは私が達成しようとしていることを翻訳する方法です。私は書く方法を正確には知らないし、あなたのような誰かを信じていた。ご指摘いただきありがとうございます。 – iCosmin

+0

あなたの問題は特別なゲージやLiveChartsとは関係ありません。良いスタックオーバーフローの問題を解決するには、問題が発生していることを示すために必要な最小限のコード量で、基本的な問題を抽出するコード例を提供する必要があります。 WPFとデータバインディングに関する多くのチュートリアルを読むことができます。それもうまくいくでしょう。 –

答えて

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); } 
} 
+2

sourceプロパティは、バインディングを動作させるために 'DependencyProperty'である必要はありません。 –

+1

@PeterDuniho sourceプロパティは、初期バインディングを動作させるための 'DependencyProperty'である必要はありません。この場合、プロパティのデフォルト値は0で、明示的に設定されています。したがって、それらは 'DepenceyProperties'として実装する必要があります。そうしないと、OPは' INotifyPropertyChanged'を実装する必要があります。 –

+0

すぐに応答していただきありがとうございます。私はこれで本当に新しいです。 DependencyPropertyを実装する方法を教えてください。どこから始めるべきかわからない。 – iCosmin

関連する問題