我是WPF和C#的新手,但由於這個社區我很快就接受了。我試圖從活動圖表中實現一個角度測量儀到我的項目中。如果只有一個綁定值並且其餘值固定,如下例所示:(https://lvcharts.net/App/examples/v1/wpf/Angular%20Gauge)。WPF Gauge中的動態值
有人可以幫我調整我的代碼,以使其動態?而不是固定值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;
}
}
}
你提的問題非常廣泛,尤其是缺乏良好的[MCVE。但是,你沒有在你的'MainWindow'類中實現'INotifyPropertyChanged'。您最好爲這些屬性創建單獨的視圖模型類,但是如果您希望綁定在綁定初始化後反映設置的值,則屬性所屬的類需要實現'INotifyPropertyChanged'。 –
彼得你好!對不起,我無法遵循指導原則。第一個例子,如果可以驗證和工作。然後第二個作爲翻譯我想要實現的方法。我不知道如何寫字,我指望像你這樣的人,他更有知識可以幫助我。感謝您的及時建議。 – iCosmin
您的問題與特殊測量儀或LiveCharts無關。爲了提出一個很好的Stack Overflow問題,你需要提供一個代碼示例,將基本問題提取到演示你遇到的問題所需的_minimal_大量代碼中。或者,您知道,您可以閱讀關於WPF和數據綁定的許多教程。這也會起作用。 –