gpt4 book ai didi

c# - MVVM 绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-12-03 10:48:46 24 4
gpt4 key购买 nike

我正在尝试将我的数据映射到 ViewModel 中的图表。我设置绑定(bind),填充对象,但由于某种原因没有显示数据。您能否验证我做错了什么?谢谢!

查看模型文件:

QueryableCollectionView qcv;
public QueryableCollectionView Groups
{
get
{
return qcv;
}
set
{
qcv = value;
}

}

public RevenueChartViewModel()
{

List<Revenue> list = new List<Revenue>
{
new Revenue{Rev = 10, Category = 1, Date = DateTime.Today.AddDays(1)},
new Revenue{Rev = 20, Category = 1, Date = DateTime.Today.AddDays(3)},
new Revenue{Rev = 30, Category = 2, Date = DateTime.Today.AddDays(6)},
};

qcv = new QueryableCollectionView(list);
qcv.GroupDescriptors.Add(new GroupDescriptor() { Member = "Category" });
}

XAML:
<UserControl x:Class="FrontEnd.RevenueChart"
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:FrontEnd"
mc:Ignorable="d" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" HorizontalAlignment="Stretch"
xmlns:telerikChart="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Charting"
xmlns:telerikCharting="clr-namespace:Telerik.Windows.Controls.Charting;assembly=Telerik.Windows.Controls.Charting"
xmlns:telerikControls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls">

<UserControl.DataContext>
<local:RevenueChartViewModel/>
</UserControl.DataContext>
<Grid>
<telerikChart:RadChart x:Name="radChart1">
<telerik:Series DataSeries="{Binding Groups}">

</telerik:Series>
<telerik:RadChart.SeriesMappings>
<telerikCharting:SeriesMapping LegendLabel="Product Sales">
<telerikCharting:SeriesMapping.SeriesDefinition>
<telerikCharting:AreaSeriesDefinition/>
</telerikCharting:SeriesMapping.SeriesDefinition>
<telerikCharting:SeriesMapping.ItemMappings>
<telerikCharting:ItemMapping DataPointMember="XCategory" FieldName="Date"/>
<telerikCharting:ItemMapping DataPointMember="YValue" FieldName="Rev"/>
</telerikCharting:SeriesMapping.ItemMappings>
</telerikCharting:SeriesMapping>
</telerik:RadChart.SeriesMappings>
</telerikChart:RadChart>

</Grid>
</UserControl>

最佳答案

如果您不关心数据更新,如果您只是显示来自其他地方的数据,我建议您将 datacontext 初始化移动到构造函数中,这将在 View 准备好之后发生,并且它将检索您的组而无需必须实现 INotifyPropertyChanged。即使集合需要更改,这也很有可能,因为一旦设置了绑定(bind),集合就会触发 CollectionChanged 事件。

关于c# - MVVM 绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15053235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com