c# - 系统参数异常 : 'Value' property was already registered by 'Point'

标签 c# .net wpf

我有一个名为 ShellViewModel 的类和一个名为 Shell.xaml 的 WPF 页面。在我的 ShellViewModel 中,我有一个函数可以从 .csv 文件读取数据并将数据显示到 Shell.xaml 中的数据网格中。数据在图表上绘制为线图。

文件读取正常,数据在数据网格上正确显示。数据结构有 25 列。第一列是数据的名称,接下来的 24 列是数据点。虽然数据在数据网格中正确显示,但图形中没有数据点。

这是我的图表的代码。我得到的错误是“

System.ArgumentException: 'Value' property was already registered by 'Point'"

注意:obsLoadForecasts 存储了显示在数据网格中的数据

图表的 ShellViewModel.cs 代码:

public void UpdateLoadChart()
{
    var forecastList = obsLoadForecasts.ToList();
    var dataList = new List<ForecastPoint>();

    forecastList.Where(a => a.ObsForecast.ZoneName == zoneName).ToList().ForEach(a =>
    {
        dataList.Add(new ForecastPoint(1, a.ObsForecast.Hr1));
        dataList.Add(new ForecastPoint(2, a.ObsForecast.Hr2));
        dataList.Add(new ForecastPoint(3, a.ObsForecast.Hr3));
        dataList.Add(new ForecastPoint(4, a.ObsForecast.Hr4));
        dataList.Add(new ForecastPoint(5, a.ObsForecast.Hr5));
        dataList.Add(new ForecastPoint(6, a.ObsForecast.Hr6));
        dataList.Add(new ForecastPoint(7, a.ObsForecast.Hr7));
        dataList.Add(new ForecastPoint(8, a.ObsForecast.Hr8));
        dataList.Add(new ForecastPoint(9, a.ObsForecast.Hr9));
        dataList.Add(new ForecastPoint(10, a.ObsForecast.Hr10));
        dataList.Add(new ForecastPoint(11, a.ObsForecast.Hr11));
        dataList.Add(new ForecastPoint(12, a.ObsForecast.Hr12));
        dataList.Add(new ForecastPoint(13, a.ObsForecast.Hr13));
        dataList.Add(new ForecastPoint(14, a.ObsForecast.Hr14));
        dataList.Add(new ForecastPoint(15, a.ObsForecast.Hr15));
        dataList.Add(new ForecastPoint(16, a.ObsForecast.Hr16));
        dataList.Add(new ForecastPoint(17, a.ObsForecast.Hr17));
        dataList.Add(new ForecastPoint(18, a.ObsForecast.Hr18));
        dataList.Add(new ForecastPoint(19, a.ObsForecast.Hr19));
        dataList.Add(new ForecastPoint(20, a.ObsForecast.Hr20));
        dataList.Add(new ForecastPoint(21, a.ObsForecast.Hr21));
        dataList.Add(new ForecastPoint(22, a.ObsForecast.Hr22));
        dataList.Add(new ForecastPoint(23, a.ObsForecast.Hr23));
        dataList.Add(new ForecastPoint(24, a.ObsForecast.Hr24));
    });

    ObsLoadChartData = dataList;
    RaisePropertyChanged("ObsLoadChartData");

    if (!(obsLoadForecasts.Exists(a => a.ObsForecast.ZoneName.Contains("Interchange"))))
    {
        try
        {
            //Peak Load Forecast values defined
            maxRTOvalue = dataList.Max(ForecastPoint => ForecastPoint.Value);
            maxRTOhour = dataList.FindIndex(ForecastPoint => ForecastPoint.Value == maxRTOvalue);
            maxRTOhour = maxRTOhour + 1;
        }
        catch
        {
            Log("Unable to determine Peak Load Forecast ");

            maxRTOvalue = 0;
            maxRTOhour = 0;
            maxRTOhour = 0;
        }
    }
}

public class ForecastPoint : DependencyObject
{
    public ForecastPoint(int hour, decimal value)
    {
        Hour = hour;
        Value = value;
    }

    public static readonly DependencyProperty _hour = DependencyProperty.Register("Hour", typeof(Int32), typeof(ForecastPoint));
    public Int32 Hour
    {
        get { return (int)GetValue(_hour); }
        set { SetValue(_hour, value); }
    }

    public static readonly DependencyProperty _value = DependencyProperty.Register("Value", typeof(decimal), typeof(Point));
    public decimal Value
    {
        get { return (decimal)GetValue(_value); }
        set { SetValue(_value, value); }
    }
}

Shell.xaml 中的代码:

<me:LoadChart x:Name="MyLoadChart" SnapsToDevicePixels="True" />

我的 LoadChart.xaml 中的代码:

<UserControl x:Class="ProbeCTO.LoadChart"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
         xmlns:chartingPrimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit"
         xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
         xmlns:me="clr-namespace:ProbeCTO"
         mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <Style TargetType="{x:Type charting:Chart}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type charting:Chart}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>

                            <datavis:Title Content="{TemplateBinding Title}" HorizontalAlignment="Left" Margin="20 0" FontWeight="Bold" Foreground="{StaticResource DefaultButtonBrush}" Style="{TemplateBinding TitleStyle}" />

                            <!-- Use a nested Grid to avoid possible clipping behavior resulting from ColumnSpan+Width=Auto -->
                            <Grid Grid.Row="1" Margin="0,15,0,15">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <chartingPrimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                                    <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                                    <Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1 0 0 1" />
                                </chartingPrimitives:EdgePanel>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<Grid>
    <charting:Chart x:Name="MyChart" Title="{Binding ObsSelectedLoadForecast.ObsForecast.ZoneName, TargetNullValue=RTO, FallbackValue=RTO}" Grid.Row="1" SnapsToDevicePixels="True" Foreground="#DDD" BorderThickness="0">
        <charting:Chart.PlotAreaStyle>
            <Style TargetType="{x:Type Grid}">
                <Setter Property="Background" Value="{x:Null}" />
            </Style>
        </charting:Chart.PlotAreaStyle>
        <charting:Chart.Axes>
            <charting:LinearAxis Orientation="X" ShowGridLines="False" Interval="1" />
            <charting:LinearAxis Orientation="Y" ShowGridLines="False" />
        </charting:Chart.Axes>
        <charting:Chart.Series>
            <charting:AreaSeries ItemsSource="{Binding ObsLoadChartData}"
                                         IndependentValuePath="Hour"
                                         DependentValuePath="Value">
            </charting:AreaSeries>
            <charting:LineSeries ItemsSource="{Binding ObsLoadChartData}"
                                         IndependentValuePath="Hour"
                                         DependentValuePath="Value">
            </charting:LineSeries>
        </charting:Chart.Series>
    </charting:Chart>
</Grid>

最佳答案

简单:您向 DependencyProperty.Register 传递了错误的参数。由于复制和粘贴,我每周做两次。

public static readonly DependencyProperty _value = 
    DependencyProperty.Register("Value", 
        typeof(decimal), 
        typeof(ForecastPoint));

最后一个参数是您代码段中的 typeof(Point),但它必须是 typeof(ForecastPoint) —— 声明类的类型,因为它是在上面的几行中注册 Hour 属性。

顺便说一下,我会将这些命名为 ValuePropertyHourProperty 而不是 _value_hour。这是惯例,也是人们期望看到的。任何试图调用 obj.SetValue(ForecastPoint.ValueProperty, value) 的人都会遇到一些不必要的小麻烦。

关于c# - 系统参数异常 : 'Value' property was already registered by 'Point' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37258396/

相关文章:

c# - 你如何阅读 lambda 表达式?

c# - 如何在不指定类型的情况下将泛型类存储在列表中?

wpf - 表格列宽和水平对齐方式

c# - 有什么方法可以自动占用WrapPanel中的空白空间吗?

c# - 空指针异常我无法解决

c# - 如何在不需要任何外部连接的 WPF 应用程序中创建内部数据库?

c# - ReportViewer.PrintDialog() 在打印到 Adob​​e PDF 时抛出异常

c# - 将两个列表连接在一起

c# - 如何在 C# 中绑定(bind)到 Grid ColumnDefinition Width

java - 在我的 Web 服务中使用大量私有(private)静态方法是不是不好的做法