2012-02-27 103 views
0

希望有人能幫助解決一些令人沮喪的問題。Silverlight數據綁定問題 - 不與POCO綁定 - Visifire

我有一個圖形(visifire),但只是把它看作一個標準的silverlight數據綁定控件。

當試圖直接綁定到包含從INotifyPropertyChanged繼承的對象(自定義)的List的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)); 
     } 
    } 
} 

我然後創建一個列表並且它把POCO類中調用ClosedSameDayList。數據填充得很好,我檢查過了。

最終設置datacontext時,什麼也沒有發生!

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; 
     } 
    } 
+0

您可以共享XAML,以便理解綁定設置嗎? – Vivek 2012-02-27 16:35:07

+0

感謝您分享XAML。我曾嘗試使用最新版本的Visifire創建POCO課程。它在這裏工作得很好。你正在使用哪個版本的Visifire?你可以分享你的ClosedSameDayList代碼嗎? – Somnath 2012-03-07 08:20:48

+0

嗨有。我正在使用Visifire 4.0.0.0。我已經使用ClosedSameDayList類的代碼更新了問題,該類是ClosedSameDay類的實例。 – Fox 2012-03-07 10:46:02

回答

0

我正在關閉此問題。通過創建DataSeries使用下面的手動數據綁定方法,一切都正常工作

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; 
}