2012-03-26 40 views
0

目前我已经从这个例子做了一个柱状图从列表框中: http://weblogs.thinktecture.com/cnagel/2011/06/wpf-listbox-as-bar-chart.html更改从代码隐藏(列表框)的DataTemplate控制(retangle填充)

什么我想要做的就是改变栏的颜色。我的图表将有5个栏,每个栏必须有不同的颜色。基本上我想改变矩形的填充。

编辑: ItemsControl的似乎不再得到任何内容,我的表是不可见的。

 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> 

最好的问候。

+1

你应该返回一个'List ',*不要*在后面的代码中创建UIElements,'ListBox'将创建ListBoxItems '为你,如果你返回'ListBoxItems','ItemTemplate' *将被忽略*,你应该看到[绑定错误](http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30 /debugging-data-bindings-in-a-wpf-or-silverlight-application.aspx)。 – 2012-03-26 12:10:21

+0

虽然我的列表似乎没有用值更新?进行第二次编辑,列出填充itemsControl的方法。但是XAML解析错误现在确实消失了,尽管我的图表现在仍然是空的。问候Rakr – Rakr 2012-03-26 12:18:16

+1

您是否为'Fill'设置了默认值?如果你不这样做,你将不会看到任何东西,因为'Rectangles'的'Fill'将会是'null'。 – 2012-03-26 12:55:26

回答

1

您应该始终避免与用于模板的控件直接交互,而不是bind需要更改为您的项目的所有内容,然后编辑该项目上的问题属性。

而是只使用值,您将需要更为复杂的对象的集合,至少是他们所需要的两个属性:

public class Bar : INotifyPropertyChanged 
{ 
    public double Value { get; set; } // Implement interface in those properties for binding 
    public Brush Fill { get; set; } 
} 

Implementing INPC

<!-- in the template --> 
<Rectangle Width="{Binding Value}" Fill="{Binding Fill}" .../> 
var bar = (Bar)lbGrafiek.Items.CurrentItem; 
bar.Fill = Brushes.Red; 
+0

你能解释一些进一步的东西吗?因为它似乎我试图解决这个问题的完全错误的方式。谢谢。 – Rakr 2012-03-26 09:17:29

+2

@Rakr:添加了一个示例 – 2012-03-26 09:23:57

+0

感谢H.B.我会看看这种新方法。 – Rakr 2012-03-26 09:29:22