我是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和数据绑定的许多教程。这也会起作用。 –