Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Changing Column Chart Bar Width Through Code
3 replies. Latest Post by JustinAngel on December 31, 2008.
(0)
tsmccartan
Member
8 points
19 Posts
12-30-2008 5:32 PM |
Can you create and set a style programatically through code? Like the following:
ChartSeriesStyle.TargetType =
ChartSeriesStyle.SetValue(
((
This crashes on me, so I don't think it is correct. Any help would be appreciated.
Thanks
lee_sl
Contributor
2992 points
585 Posts
12-31-2008 2:31 AM |
try
12-31-2008 9:20 AM |
Thanks for the reply. Unfortunatly, this still causes the same error:
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
I have another related question. Is there a way to change the individual bars in a chart to a different color, through code? I have to highlight a specific bar, based on its values after binding.
Thanks again,
Tom
JustinAngel
4415 points
596 Posts
12-31-2008 3:09 PM |
Here's the XAML code needed to set the style:
<charting:Chart x:Name="chart">
<charting:ColumnSeries ItemsSource="{Binding}"
DependentValueBinding="{Binding Value}"
IndependentValueBinding="{Binding Key}">
<charting:ColumnSeries.DataPointStyle>
<Style TargetType="charting:ColumnDataPoint">
<Setter Property="MaxWidth" Value="20" />
</Style>
</charting:ColumnSeries.DataPointStyle>
</charting:ColumnSeries>
</charting:Chart>
chart.DataContext = new KeyValuePair<string, int>[]
{
new KeyValuePair<string, int>("hello", 1),
new KeyValuePair<string, int>("world", 2),
new KeyValuePair<string, int>("foo", 3),
new KeyValuePair<string, int>("bar", 4)
};
If you want to specify the DataPointStyle in code you'll have to use the StylePalette on the chart.
public SilverlightControl32()
InitializeComponent();
System.Windows.Style DataPointStyle = new Style(typeof(ColumnDataPoint));
DataPointStyle.Setters.Add(new Setter(ColumnDataPoint.MaxWidthProperty, 10.0));
StylePalette palette = new StylePalette() { DataPointStyle };
chart.StylePalette = palette;
this.Loaded += new RoutedEventHandler(SilverlightControl32_Loaded);
}
void SilverlightControl32_Loaded(object sender, RoutedEventArgs e)
There seems to be a bug setting the DataPointStyle once the series has been initialized from XAML, so working through the StylePalette is your best bet. I've opened up a Silverlight Toolkit codeplex issue to document that DataPointStyle itself is not respected when set from code: http://www.codeplex.com/Silverlight/WorkItem/View.aspx?WorkItemId=1302 If you'd like to see this issue fixed, please feel free to vote on it.