【发布时间】:2012-02-27 14:04:20
【问题描述】:
希望有人可以帮助解决一些令人沮丧的问题。
我有一个图表 (visifire),但只是将其视为标准的 silverlight 数据绑定控件。
当尝试直接绑定到包含从 INotifyPropertyChanged 继承的对象列表 的 POCO 对象时,绝对不会发生任何事情!
这里是我的字典条目类
class GraphValue : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string IndicatorName
{
get
{
return name;
}
set
{
name = value;
onPropertyChanged(this, "IndicatorName");
}
}
private double _value;
public double IndicatorValue
{
get
{
return _value;
}
set
{
_value = value;
onPropertyChanged(this, "IndicatorValue");
}
}
private void onPropertyChanged(object sender, string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
}
然后我创建一个列表并将其放入一个名为 ClosedSameDayList 的 POCO 类中。数据填充得很好,我已经检查过了。
当最终设置数据上下文时,什么也没有发生!
MyGraph.DataContext = ClosedSameDayList.GraphValues.OrderBy(z => z.IndicatorName);
然而,这是关键。
当执行以下操作时,一切正常:
ObservableCollection<KeyValuePair<string, double>> g = new ObservableCollection<KeyValuePair<string, double>>();
foreach (var item in ClosedSameDayList.GraphValues)
{
g.Add(new KeyValuePair<string, double>(item.IndicatorName, item.IndicatorValue));
}
MyGraph.DataContext = g.OrderBy(z => z.Key);
在此附上图表的 XAML:
<vc:Chart xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts" DataContext="{Binding}" Name="MyGraph" Height="240" BorderThickness="0" Theme="Theme2" View3D="True" ToolBarEnabled="True" >
<vc:Chart.Titles>
<vc:Title Text="My Title" />
</vc:Chart.Titles>
<vc:Chart.AxesX>
<vc:Axis Title="My Title" />
</vc:Chart.AxesX>
<vc:Chart.AxesY>
<vc:Axis Title="My Title" AxisType="Primary" />
</vc:Chart.AxesY>
<vc:Chart.Series>
<vc:DataSeries RenderAs="Column" DataSource="{Binding}">
<vc:DataSeries.DataMappings>
<vc:DataMapping MemberName="AxisXLabel" Path="IndicatorName"></vc:DataMapping>
<vc:DataMapping MemberName="YValue" Path="IndicatorValue"></vc:DataMapping>
</vc:DataSeries.DataMappings>
</vc:DataSeries>
</vc:Chart.Series>
</vc:Chart>
在此附上 ClosedSameDay 的代码
class ClosedSameDay
{
public CamlQuery CamlQuery { get; set; }
public List List { get; set; }
public ListItemCollection Listitems { get; set; }
public List<GraphValue> GraphValues { get; set; }
public ClosedSameDay()
{
GraphValues = new List<GraphValue>();
CamlQuery = new CamlQuery();
CamlQuery.ViewXml = "<View>" +
" <Method Name='ITServicedesk_Dashboard_ClosedSameday_Individuals_Readlist'/>" +
" <Query>" +
" <OrderBy>" +
" <FieldRef Name='id'/>" +
" </OrderBy>" +
" </Query>" +
" <ViewFields>" +
" <FieldRef Name='id'/>" +
" <FieldRef Name='Name'/>" +
" <FieldRef Name='Total'/>" +
" </ViewFields>" +
"</View>";
}
public void PopulateObjectData()
{
foreach (ListItem item in Listitems)
{
double value = -1;
double.TryParse(item["Total"].ToString(), out value);
if (item["Name"] != null && !string.IsNullOrEmpty(item["Name"].ToString()) && value != -1)
{
this.GraphValues.Add(new GraphValue
{
IndicatorName = item["Name"].ToString(),
IndicatorValue = value
});
}
}
}
public DataSeries BuildDataSeries()
{
ObservableCollection<KeyValuePair<string, double>> g = new ObservableCollection<KeyValuePair<string, double>>();
foreach (var item in this.GraphValues)
{
g.Add(new KeyValuePair<string, double>(item.IndicatorName, item.IndicatorValue));
}
Visifire.Charts.DataSeries ds = new Visifire.Charts.DataSeries();
ds.RenderAs = Visifire.Charts.RenderAs.Column;
ds.DataSource = g.OrderBy(i => i.Key);
Visifire.Charts.DataMapping dm = new Visifire.Charts.DataMapping();
dm.MemberName = "AxisXLabel";
dm.Path = "Key";
ds.DataMappings.Add(dm);
Visifire.Charts.DataMapping dm2 = new Visifire.Charts.DataMapping();
dm2.MemberName = "YValue";
dm2.Path = "Value";
ds.DataMappings.Add(dm2);
return ds;
}
}
【问题讨论】:
-
能否分享 XAML 以便于理解绑定设置?
-
感谢分享 XAML。我尝试使用最新版本的 Visifire 使用 POCO 类创建相同的内容。它在这里运作良好。您使用的是哪个版本的 Visifire?你能分享你的 ClosedSameDayList 代码吗?
-
您好。我正在使用 Visifire 4.0.0.0 。我已经用 ClosedSameDayList 类的代码更新了这个问题,它是 ClosedSameDay 类的一个实例。
-
您在 XAML 中添加了 DataSeries,还添加了 DataSeries 表单代码?公共数据系列 BuildDataSeries()!我很困惑!请编辑您的问题,以免造成混淆。我正在尝试找出问题所在。我猜 DataMapping 时 Path 属性设置不正确;根据 XAML,它应该是 dm.Path = "IndicatorName";和下一个 dm.Path = "IndicatorValue";
-
感谢您尝试帮助 Somnath。我在后面的代码中做了 DataSeries,因为 XAML 绑定不起作用。无论如何,我找到了解决方法,所以我会坚持下去。投票支持尝试提供帮助。
标签: silverlight visifire