簡體   English   中英

WPF 實時圖表 - XAML 代碼未鏈接到 C# 代碼

[英]WPF Live Charts - XAML code not linking to C# code

我正在為實時圖表而苦苦掙扎。 我正在使用 WPF。

我想建立一個條形圖,按腰帶顏色顯示空手道俱樂部的成員數量。 我的學習項目之一。

按照他們的文檔: https://lvcharts.net/App/examples/v1/wpf/Basic%20Column

我在 xaml 中遇到錯誤:

d:DataContext="{d:DesignInstance local:Charts}" '名稱 "Charts" 不存在於命名空間 "clr-namespace:KarateClub" '

LabelFormatter="{Binding Formatter}" '在圖表類型中找不到方法格式化程序'

如果我刪除 DataContext 代碼,圖表會顯示,但它不使用我的任何值。 我一定錯過了如何將 XAML 代碼鏈接到 C# 代碼......? 我的類/命名空間路徑錯誤嗎?

我的 XAML 代碼:

<UserControl x:Class="KarateClub.Charts"
             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:local="clr-namespace:KarateClub"
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="1000" d:DataContext="{d:DesignInstance local:Charts}" >

    <Grid Background="White" Width="1000" Height="550">
        <Grid Background="White" Margin="33,45,239,170">
            <lvc:CartesianChart Series="{Binding SeriesCollection}" LegendLocation="Left">
                <lvc:CartesianChart.AxisX>
                    <lvc:Axis Title="Belts" Labels="{Binding Labels}"></lvc:Axis>
                </lvc:CartesianChart.AxisX>
                <lvc:CartesianChart.AxisY>
                    <lvc:Axis Title="Members" LabelFormatter="{Binding Formatter}"></lvc:Axis>
                </lvc:CartesianChart.AxisY>
            </lvc:CartesianChart>
        </Grid>
    </Grid>
</UserControl>

我的 C# 代碼:

using System;
using System.Windows.Controls;
using LiveCharts;
using LiveCharts.Wpf;

namespace KarateClub
{
    public partial class Charts : UserControl
    {

        public SeriesCollection SeriesCollection { get; set; }
        public string[] Labels { get; set; }
        public Func<double, string> Formatter { get; set; }


        public Charts()
        {
            InitializeComponent();

            SeriesCollection = new SeriesCollection
            {
                new ColumnSeries
                {
                    Title = "2020",
                    Values = new ChartValues<double> { 1, 5, 3, 5, 7, 3, 9, 2, 3 }
                }
            };

            Labels = new[] { "White", "Yellow", "Orange", "Green", "Blue", "Purple", "Brown", "Red", "Black" };
            Formatter = value => value.ToString("N");

            DataContext = this;

        }
    }
}

您當前正在使用由設計器生成的Charts的設計時實例。 此實例是自動生成的,不包含任何數據。 這是默認行為,由標記擴展DesignInstanceExtensionIsDesignTimeCreatable屬性控制。 默認情況下IsDesignTimeCreatable返回false ,它指示設計者使用反射(繞過任何構造函數)創建一個假實例。

要使用正確構造的指定類型的設計時實例,您必須將此屬性顯式設置為true

<UserControl x:Class="KarateClub.Charts"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:KarateClub"
             d:DataContext="{d:DesignInstance local:Charts, IsDesignTimeCreatable=True}" >
  ...
</UserControl>

現在設計器將使用構造函數而不是反射來創建實例。

顯然,這個 Blend 設計時屬性沒有得到很好的記錄。 要了解更多信息,請參閱Microsoft Docs:Silverlight 設計器中的設計時屬性

您遵循的示例沒有說d:DataContext="{d:DesignInstance local:Charts}"

它說d:DataContext="{d:DesignInstance local:BasicColumn}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM