Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PieChart没有出现_C#_Wpf_Xaml_Charts - Fatal编程技术网

C# PieChart没有出现

C# PieChart没有出现,c#,wpf,xaml,charts,C#,Wpf,Xaml,Charts,我需要显示我当前正在使用的图表。我确实复制了文档中的代码,问题是我总是在屏幕上显示边框和标题,但没有图表 XAML <UserControl x:Class="Projet.Recources0.Statistique.Ad_Aj" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/20

我需要显示我当前正在使用的
图表
。我确实复制了文档中的代码,问题是我总是在屏幕上显示边框和标题,但没有图表

XAML

<UserControl x:Class="Projet.Recources0.Statistique.Ad_Aj"
         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:mui="http://firstfloorsoftware.com/ModernUI"
         xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
         xmlns:chart="clr-namespace:De.TorstenMandelkow.MetroChart;assembly=De.TorstenMandelkow.MetroChart"
         mc:Ignorable="d" d:DesignWidth="1000" Height="670">

<UserControl.Resources>
    <Style x:Key="MinimalChartStyle" TargetType="{x:Type chart:ChartBase}">
        <Setter Property="Width" Value="500"/>
        <Setter Property="Height" Value="500"/>            
    </Style>
</UserControl.Resources>
<Grid >
    <chart:PieChart
    Style="{StaticResource MinimalChartStyle}"
    ChartTitle="Minimal Pie Chart"
    ChartSubTitle="Chart with fixed width and height"
    SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" >
        <chart:PieChart.Series>
            <chart:ChartSeries
            SeriesTitle="Errors"
            DisplayMember="Category"
            ValueMember="Number"
            ItemsSource="{Binding Path=Errors}" />
        </chart:PieChart.Series>
    </chart:PieChart>
</Grid>

CS

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Projet.Recources0.Statistique
{
    /// <summary>
    /// Interaction logic for Ad_Aj.xaml
    /// </summary>

    public partial class Ad_Aj : UserControl
    {
        public ObservableCollection<TestClass> Errors { get; private set; }

        public Ad_Aj()
        {
            Errors = new ObservableCollection<TestClass>();
            Errors.Add(new TestClass() { Category = "Globalization", Number = 75 });
            Errors.Add(new TestClass() { Category = "Features", Number = 2 });
            Errors.Add(new TestClass() { Category = "ContentTypes", Number = 12 });
            Errors.Add(new TestClass() { Category = "Correctness", Number = 83});
            Errors.Add(new TestClass() { Category = "Best Practices", Number = 29 });
        }

        private object selectedItem = null;
        public object SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                // selected item has changed
                selectedItem = value;                
            }
        }
    }

    // class which represent a data point in the chart
    public class TestClass
    {
        public string Category { get; set; }

        public int Number { get; set; }
    }
}
使用MySql.Data.MySqlClient;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统配置;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间Projet.resources0.Statistique
{
/// 
///Ad_Aj.xaml的交互逻辑
/// 
公共部分类Ad_Aj:UserControl
{
公共可观测收集错误{get;private set;}
公共广告
{
错误=新的ObservableCollection();
添加(newtestclass(){Category=“Globalization”,Number=75});
添加(newtestclass(){Category=“Features”,Number=2});
添加(newtestclass(){Category=“ContentTypes”,Number=12});
添加(newtestclass(){Category=“correction”,Number=83});
添加(newtestclass(){Category=“Best Practices”,Number=29});
}
私有对象selectedItem=null;
公共对象SelectedItem
{
得到
{
返回selectedItem;
}
设置
{
//所选项目已更改
选择editem=值;
}
}
}
//类,该类表示图表中的数据点
公共类TestClass
{
公共字符串类别{get;set;}
公共整数{get;set;}
}
}

我不是控件方面的专家,但我猜您的
ItemsSource
绑定失败了。
Errors
集合作为属性在用户控件上,您对
ItemsSource
的绑定默认情况下将脱离
DataContext
,我认为在您的情况下该绑定为空。要通过绑定直接获取控件,您可能会通过执行以下操作(假设您将“local”映射到Ad_Aj所在的名称空间),使其与
RelativeSource
标记扩展一起工作:


我认为您应该将
错误
信息放在视图模型中,然后将其设置为数据上下文并使用绑定,因为
错误
信息实际上是数据,与UI分离。

创建一个
视图模型
来保存图表的数据并将其分配给用户
DataContext
如下所示:

XAML:

政务司司长:

视图模型:

public class PieChartViewModel
{
    public ObservableCollection<TestClass> Errors { get; private set; }

    public PieChartViewModel()
    {
        Errors = new ObservableCollection<TestClass>();
        Errors.Add(new TestClass() { Category = "Globalization", Number = 75 });
        Errors.Add(new TestClass() { Category = "Features", Number = 2 });
        Errors.Add(new TestClass() { Category = "ContentTypes", Number = 12 });
        Errors.Add(new TestClass() { Category = "Correctness", Number = 83 });
        Errors.Add(new TestClass() { Category = "Best Practices", Number = 29 });
    }
}
public class PieChartViewModel
{
    public ObservableCollection<TestClass> Errors { get; private set; }

    public PieChartViewModel()
    {
        Errors = new ObservableCollection<TestClass>();
    }
}
公共类PieChartViewModel
{
公共可观测收集错误{get;private set;}
公共视图模型()
{
错误=新的ObservableCollection();
}
}

所以你走对了方向!您只缺少一行代码

public partial class Ad_Aj : UserControl
{
    public ObservableCollection<TestClass> Errors { get; private set; }

    public Ad_Aj()
    {
        /*
         * ----------------------------
         * This is line you're missing.
         * ----------------------------
         */
        DataContext = this;
        /*
         * ----------------------------
         */
        Errors = new ObservableCollection<TestClass>();
        Errors.Add(new TestClass() { Category = "Globalization", Number = 75 });
        Errors.Add(new TestClass() { Category = "Features", Number = 2 });
        Errors.Add(new TestClass() { Category = "ContentTypes", Number = 12 });
        Errors.Add(new TestClass() { Category = "Correctness", Number = 83});
        Errors.Add(new TestClass() { Category = "Best Practices", Number = 29 });
    }
}
public部分类Ad_Aj:UserControl
{
公共可观测收集错误{get;private set;}
公共广告
{
/*
* ----------------------------
*这是你错过的线路。
* ----------------------------
*/
DataContext=this;
/*
* ----------------------------
*/
错误=新的ObservableCollection();
添加(newtestclass(){Category=“Globalization”,Number=75});
添加(newtestclass(){Category=“Features”,Number=2});
添加(newtestclass(){Category=“ContentTypes”,Number=12});
添加(newtestclass(){Category=“correction”,Number=83});
添加(newtestclass(){Category=“Best Practices”,Number=29});
}
}
这是MVVM,但不是真正的MVVM。真正的MVVM有一个单独的ViewModel类。这里要做的是使用视图的代码作为ViewModel。这是有效的,没有人会在这件事上和你较量。然而,如果您试图实现真正的MVVM,那么您需要分离出您的类

如何在XAML中将DataContext(也称为“绑定”)设置为ViewModel

<Window.DataContext>
    <local:PieChartViewModel />
</Window.DataContext>


但是,请注意,他使用的是一个单独的
ViewModel
类。您在问题中提供的代码不会执行此操作,因此我不确定如何以相同的方式执行此操作。另外,值得一提的是,如果您的
ViewModel
类使用构造函数依赖项注入或具有参数,那么您将不得不使用一些魔法来实现这一点。如果是这样的话,只需在构造函数中设置它就更容易了。

之所以这样做,是因为您正在设置
UserControl
DataContext
。这是原始海报代码中缺少的部分。这是标准的
MVVM
。这是正确的。但您也可以将控件的DataContext绑定到其自身,这正是原始海报所要做的。您提供了有效的代码,但没有明确说明原始海报问题的答案。:)如何创建视图模型请参见我文章中关于动态创建和填充
ViewModel
的编辑。
public partial class Window2 : Window
{
    PieChartViewModel viewModel;

    public Window2()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        viewModel = new PieChartViewModel();

        viewModel.Errors.Add(new TestClass() { Category = "Globalization", Number = 75 });
        viewModel.Errors.Add(new TestClass() { Category = "Features", Number = 2 });
        viewModel.Errors.Add(new TestClass() { Category = "ContentTypes", Number = 12 });
        viewModel.Errors.Add(new TestClass() { Category = "Correctness", Number = 83 });
        viewModel.Errors.Add(new TestClass() { Category = "Best Practices", Number = 29 });

        DataContext = viewModel;
    }
}
public class PieChartViewModel
{
    public ObservableCollection<TestClass> Errors { get; private set; }

    public PieChartViewModel()
    {
        Errors = new ObservableCollection<TestClass>();
    }
}
public partial class Ad_Aj : UserControl
{
    public ObservableCollection<TestClass> Errors { get; private set; }

    public Ad_Aj()
    {
        /*
         * ----------------------------
         * This is line you're missing.
         * ----------------------------
         */
        DataContext = this;
        /*
         * ----------------------------
         */
        Errors = new ObservableCollection<TestClass>();
        Errors.Add(new TestClass() { Category = "Globalization", Number = 75 });
        Errors.Add(new TestClass() { Category = "Features", Number = 2 });
        Errors.Add(new TestClass() { Category = "ContentTypes", Number = 12 });
        Errors.Add(new TestClass() { Category = "Correctness", Number = 83});
        Errors.Add(new TestClass() { Category = "Best Practices", Number = 29 });
    }
}
<Window.DataContext>
    <local:PieChartViewModel />
</Window.DataContext>