首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF实时图表-未链接到C#代码的XAML代码

问WPF实时图表-未链接到C#代码的XAML代码
EN

Stack Overflow用户
提问于 2020-06-11 09:43:21
回答 2查看 918关注 0票数 1

我正在努力处理实时图表。我使用的是WPF。

我想构建一个条形图,按腰带颜色显示空手道俱乐部的会员数量。我的一个学习项目。

遵循他们的文档:https://lvcharts.net/App/examples/v1/wpf/Basic%20Column

我在xaml中遇到错误:

d:DataContext="{d:DesignInstance local:Charts}“‘名称”图表“在命名空间"clr- namespace :KarateClub”中不存在’

和

LabelFormatter="{Binding Formatter}“‘类型图表中找不到方法格式化程序’

如果我删除DataContext代码,图形将会显示,但它不使用我的任何值。我一定是错过了如何将XAML代码链接到C#代码...?我是不是把类/命名空间路径弄错了?

我的XAML代码:

代码语言:javascript
复制
<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#代码:

代码语言:javascript
复制
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;

        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-12 00:54:00

您当前正在使用由设计器生成的Charts的设计时实例。此实例是自动生成的,不包含任何数据。这是默认行为,由标记扩展DesignInstanceExtension的IsDesignTimeCreatable属性控制。默认情况下,IsDesignTimeCreatable返回false,它指示设计器使用反射创建一个假实例(绕过任何构造函数)。

若要使用正确构造的指定类型的设计时实例,必须将此属性显式设置为true

代码语言:javascript
复制
<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>

现在设计器将使用构造函数而不是反射来创建一个实例。

显然,这种混合的设计时属性没有很好的文档记录。要了解更多信息,请参阅Microsoft Docs: Design-Time Attributes in the Silverlight Designer

票数 1
EN

Stack Overflow用户

发布于 2020-06-11 09:57:37

您正在遵循的示例并没有指定d:DataContext="{d:DesignInstance local:Charts}"。

上面写的是d:DataContext="{d:DesignInstance local:BasicColumn}"。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62315770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档