Currently i have made a bar chart from a listbox from this example:
http://weblogs.thinktecture.com/cnagel/2011/06/wpf-listbox-as-bar-chart.html
What i want to do is change the color of the bars. My chart will have 5 bars and each have to be in a different color. Basically i want to change the Fill of the Rectangle.
EDIT:
The ItemsControl no longer seems to be getting any content and my chart is invisible.
public class GrafiekBar : INotifyPropertyChanged
{
private int value;
private Brush fill = Brushes.Blue;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public List<GrafiekBar> GetDataGrafiek()
{
int[] getallen = new int[] { 3, 5, 8, 6, 2 };
List<GrafiekBar> list = new List<GrafiekBar>();
for (int i = 0; i < getallen.Length; i++)
{
GrafiekBar bar = new GrafiekBar();
bar.Value = getallen[i];
bar.Fill = Brushes.Blue;
list.Add(bar);
}
return list;
}
/// <summary>
/// Implement INotifyPropertyChanged in properties
/// http://msdn.microsoft.com/en-us/library/ms229614.aspx
/// </summary>
public int Value
{
get { return this.value; }
set
{
if (value != this.value)
{
this.value = value;
NotifyPropertyChanged("Value");
}
}
}
public Brush Fill
{
get { return this.fill; }
set
{
if (value != this.fill)
{
this.fill = value;
NotifyPropertyChanged("Fill");
}
}
}
}
XAML:
<ObjectDataProvider x:Key="odpLbGrafiek" ObjectType="{x:Type myClasses:GrafiekBar}" MethodName="GetDataGrafiek"/>
<ObjectDataProvider x:Key="odpLbGrafiekColors" ObjectType="{x:Type myClasses:GrafiekBar}" MethodName="Fill"/>
<DataTemplate x:Key="GrafiekItemTemplate">
<Border Width="Auto" Height="Auto">
<Grid>
<Rectangle x:Name="recGrafiek" Fill="{Binding Source={StaticResource odpLbGrafiekColors}}" StrokeThickness="0"
Height="45" Width="{Binding}" Margin="13.2"
HorizontalAlignment="Right" VerticalAlignment="Bottom">
<Rectangle.LayoutTransform>
<ScaleTransform ScaleX="20" />
</Rectangle.LayoutTransform>
</Rectangle>
</Grid>
</Border>
</DataTemplate>
<ItemsControl x:Name="icGrafiek"
Margin="0"
ItemsSource="{Binding Source={StaticResource odpLbGrafiek}}"
ItemTemplate="{DynamicResource GrafiekItemTemplate}"
Grid.RowSpan="5"
Grid.Column="1" Background="{x:Null}" HorizontalAlignment="Left" RenderTransformOrigin="0.5,0.5"
>
<ItemsControl.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="180"/>
<TranslateTransform/>
</TransformGroup>
</ItemsControl.RenderTransform>
</ItemsControl>
Best regards.
You should always avoid direct interaction with the controls used for templating, instead bind everything you need to change to your item, then edit the property in question on the item.
Instead of using just the values you will need a collection of more complex objects, at the very least they need two properties:
(Implementing
INPC)