Devo visualizzare un PieChart
, Attualmente sto usando Modern UI (Metro) Charts. Ho copiato il codice nella documentazione e il problema è che ho sempre il bordo e il titolo sullo schermo ma nessun grafico.PieChart non viene visualizzato
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; }
}
}
Il motivo per cui questo funziona è perché si sta impostando il 'DataContext' di' UserControl'. Questo è il pezzo che manca nel codice del poster originale. – Cameron
Questo è lo standard 'MVVM'. – jsanalytics
Questo è corretto. Ma puoi anche associare DataContext di un controllo a se stesso, che è ciò che il poster originale sta tentando di fare. Hai dato il codice che funziona, ma non hai detto esplicitamente quale sia la risposta al problema del poster originale. :) – Cameron