简体   繁体   中英

Silverlight Databinding issue - Not binding to POCO - Visifire

Hope someone can help with a bit of a frustrating issue.

I have a Graph (visifire) but just look at it as a standard silverlight databound control.

When attempting to bind directly to my POCO Object which contains a List<> of objects (custom) which inherits from INotifyPropertyChanged, then absolutely nothing happens!

Herewith my Dictionary Entry Class

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

I then create a List and it's put inside a POCO Class called ClosedSameDayList. The data is populated fine, i've checked.

When eventually setting the datacontext, nothing happens!

MyGraph.DataContext = ClosedSameDayList.GraphValues.OrderBy(z => z.IndicatorName);

However, here's the kicker.

When doing the following, everything works :

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

Herewith the XAML for the Chart:

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

Herewith Code for 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;
        }
    }

尝试使用 Visifire 示例区域中“ DataBinding快速入门”示例应用程序

Im closing this Question. Using the manual databinding method below by creating a DataSeries, everything worked fine

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM