XAML列表<lineseries>绑定到图表系列</lineseries>

时间:2014-10-13 14:02:44

标签: c# xaml mvvm charts toolkit

我有MVVM silverlight应用程序和工具包图表。 在视图模型中,我创建了ObservableCollection属性:

    private ObservableCollection<LineSeries> _lines = new ObservableCollection<LineSeries>();
    public ObservableCollection<LineSeries> Lines
    {
        get { return _lines; }
        set
        {
            _lines = value;
            NotifyPropertyChanged("Lines");
        }
    }

然后在某些方法中,我使用动态计数行填充此集合:

            List<SolidColorBrush> colors = BS2ColorSetHelper.GetSetColors();
            for (int i = 0; i < remainderData.Count; i++)
            {
                LineSeries line = (colors.ElementAtOrDefault(i) != null)
                    ? CreateNewLineSeriesWithColor(remainderData[i].DenominationName, remainderData[i].Coords, colors[i])
                    : CreateNewLineSeries(remainderData[i].DenominationName, remainderData[i].Coords);

                line.Name = remainderData[i].DenominationName;
                Lines.Add(line);
            }
            .........

现在我想将此ObservableCollection绑定到工具包图表系列。

    <toolkit:Chart Name="chart">
        <toolkit:Chart.Series>
            ????
        </toolkit:Chart.Series>
    </toolkit:Chart>

我试过了

系列=&#34; {Binding Path = Lines}&#34;

但它不起作用。 Visual Studio显示错误:类型为#System.SWindows.Data.Binding&#39;无法转换为类型&#39; System.Collections.ObjectModel.Collection`1 [System.Windows.Controls.DataVisuali zation.Charting.ISeries]&#39;。我认为这是因为Series不是依赖属性。

2 个答案:

答案 0 :(得分:0)

我不完全了解有关控件的信息Chart我建议您查看控件创建者提供的文档,以找出可绑定属性的名称。

但是根据你发布的内容我的猜测是尝试:

<toolkit:Chart Name="chart" Series={Binding Lines}/>

猜测您的Datacontext已全部到位。意味着页面的datacontext设置为viewmodel

答案 1 :(得分:0)

好的,我们无法将LineSeries绑定到Series,因为Series不是Dependency属性。 因此,我们可以使用此依赖项属性创建新的UserControl:

public class MultiChart : Chart
{
    public IEnumerable SeriesSource
    {
        get
        {
            return (IEnumerable)GetValue(SeriesSourceProperty);
        }
        set
        {
            SetValue(SeriesSourceProperty, value);
        }
    }

    public static readonly DependencyProperty SeriesSourceProperty = DependencyProperty.Register(
        name: "SeriesSource",
        propertyType: typeof(IEnumerable),
        ownerType: typeof(MultiChart),
        typeMetadata: new PropertyMetadata(
            defaultValue: default(IEnumerable),
            propertyChangedCallback: new PropertyChangedCallback(OnSeriesSourceChanged)
        )
    );

    private static void OnSeriesSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        IEnumerable newValue = (IEnumerable)e.NewValue;
        MultiChart source = (MultiChart)d;

        source.Series.Clear();

        foreach (LineSeries item in newValue)
        {
            source.Series.Add(item);
        }
    }
}

然后我们将LineSeries绑定到新创建的属性:

    <common:MultiChart Name="chart"
                       Title="{Binding Path=Title}"
                       SeriesSource="{Binding Path=Lines}" />

视图模型将是:

public class ChartDenominationViewModel : ViewModel
{
    private string _title;

    public string Title
    {
        get { return _title; }
        set
        {
            _title = value;
            NotifyPropertyChanged("Title");
        }
    }

    private ObservableCollection<LineSeries> _lines = new ObservableCollection<LineSeries>();

    public ObservableCollection<LineSeries> Lines
    {
        get { return _lines; }
        set
        {
            _lines = value;
            NotifyPropertyChanged("Lines");
        }
    }
}