Skredy Posted July 22, 2011 Report Share Posted July 22, 2011 Hi, I would like to change the background color of several DataView cells, depending on values from two different series. I tried to create a new serie, wich contains the values of the two others, and reference it in my DataView (with his index). But I dont find how to get the right DataItem. Here is my snippet: => where i reference the new serie in the Chart's SeriesAttributesCollection ItemsSource="{Binding ValuesAndInfluence}" Visibility="Collapsed" BindingPathX="Datetime" BindingPath="Value" > => in the DataView Text="{Binding Path=????}" VerticalAlignment="Center" HorizontalAlignment="Right" Padding="5 2 5 2" /> If I could get the right dataItem, I would finish with a converter for the backgroundcolor... Thanks for your help! Quote Link to comment Share on other sites More sharing options...
JuanC Posted July 27, 2011 Report Share Posted July 27, 2011 I think the snippet got lost in your posting but this is a sample of how to change the background color of your dataview cells, the key of the sample is that in most logical items we generate (and you can style to change the appearance of the bars, legend items, etc.) the DataItem property will typically point to the data you provided to the chart. Data Model public class BusinessData{ public double Y1 { get; set; } public double Y2 { get; set; } } XAML to customize chart, note that we use SeriesField, because of this you will automatically get some nice behaviors, for example if your axis was formatted as currency the ValueText property would reflect that, you could also have used DataField which can be used to place a field from your data even if it is not plotted as a series. Obviously you might want to place the template separately but this helps on sample simplicity. <cfx:Chart.DataView> <cfx:DataViewVisibility="Visible"> <cfx:DataView.Fields> <cfx:DataViewFieldAttributes> <cfx:DataViewFieldAttributes.Items> <cfx:SeriesFieldSeriesIndex="0"> <cfx:SeriesField.Template> <DataTemplate> <DataTemplate.Resources> <local:BusinessDataColorConverter x:Key="businessDataColorConverter"/> </DataTemplate.Resources> <TextBlock Text="{Binding Path=ValueText}"Background="{Binding Path=DataItem,Converter={StaticResourcebusinessDataColorConverter}}" Margin="{Binding Path=Margin}"Foreground="{Binding Path=Foreground}"FontFamily="{Binding Path=FontFamily}"FontWeight="{Binding Path=FontWeight}"FontSize="{Binding Path=FontSize}"FontStyle="{Binding Path=FontStyle}" HorizontalAlignment="Right"VerticalAlignment="Center"/> <DataTemplate.Triggers> <DataTriggerBinding="{Binding Path=Dimmed}"> <DataTrigger.Value> <sys:Boolean>True</sys:Boolean> </DataTrigger.Value> <SetterProperty="Opacity" Value="0.25"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </cfx:SeriesField.Template> </cfx:SeriesField> </cfx:DataViewFieldAttributes.Items> </cfx:DataViewFieldAttributes> </cfx:DataView.Fields> </cfx:DataView> </cfx:Chart.DataView> Code to populate chart List<BusinessData> list = newList<BusinessData>(); list.Add(newBusinessData() { Y1 = 10, Y2 = 12 }); list.Add(newBusinessData() { Y1 = 14, Y2 = 11 }); list.Add(newBusinessData() { Y1 = 11, Y2 = 17 }); chart1.Series.Add(new SeriesAttributes("Y1")); chart1.Series.Add(new SeriesAttributes("Y2")); chart1.ItemsSource = list; Color Converter object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture{ BusinessData businessData = (BusinessData) value; if (businessData.Y1 > businessData.Y2) return Brushes.Yellow; else return Brushes.White; }Regards, JuanC Quote Link to comment Share on other sites More sharing options...
Skredy Posted July 28, 2011 Author Report Share Posted July 28, 2011 Oups, you're right the snippet disappeared... :/ I tried your solution, it's perfect! In fact, my series were independent each from the others (not in the same objet and binded to SeriesAttribute with ItemSource), so I added an objet like your BusinessData wich contains all of the collections. Thanks for your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.