【问题标题】:BarChart Values not updating条形图值未更新
【发布时间】:2012-07-31 02:05:28
【问题描述】:

我正在尝试开发一个应用程序,我想在每三秒后生成一个随机数,将该数字插入一个列表框并使用 DataTemplate 将列表框显示为一个矩形。

This供参考。

现在的问题是我使用了一个 DispatcherTimer,它在 3 秒后“滴答”,但矩形没有更新。

我正在发布我的 XAML 和 .cs 代码。有什么提示吗?

namespace ListBarGraph
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window
    {
        DispatcherTimer dt = new DispatcherTimer();
        DataFactory df = new DataFactory();

        public MainWindow()
        {
            InitializeComponent();
            dt.Tick += new EventHandler(dt_Tick);
            dt.Interval = new TimeSpan(0, 0, 3);
            dt.Start();

            this.PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);
        }

        void dt_Tick(object sender, EventArgs e)
        {
            df.GetData();
        }
    }

    public class DataFactory
    {
        int number = 0;

        public IEnumerable<int> GetData()
        {
            Random random = new Random();
            number = random.Next(0, 100);
            return new int[] { 0, number };
        }
    }
}




<Window x:Class="ListBarGraph.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ListBarGraph"
        Title="MainWindow" Height="350" Width="525">

   <Window.Resources>

       <ObjectDataProvider x:Key="someData" ObjectType="{x:Type local:DataFactory}" MethodName="GetData" />

      <DataTemplate x:Key="BarChartItemsTemplate">
         <Border Width="300" Height="50">
            <Grid>
               <Rectangle Fill="Red" StrokeThickness="2" Height="40" Width="{Binding}" HorizontalAlignment="Right" VerticalAlignment="Bottom">
                  <Rectangle.LayoutTransform>
                     <ScaleTransform ScaleX="1.5"/>
                  </Rectangle.LayoutTransform>
               </Rectangle>
         </Grid>
         </Border>
      </DataTemplate>

      <ItemsPanelTemplate x:Key="BarChartItemsPanel">
         <VirtualizingStackPanel IsItemsHost="True">
            <VirtualizingStackPanel.LayoutTransform>
               <TransformGroup>
                  <RotateTransform Angle="90"/>
                  <ScaleTransform ScaleX="-1" ScaleY="1"/>
               </TransformGroup>
            </VirtualizingStackPanel.LayoutTransform>
         </VirtualizingStackPanel>
      </ItemsPanelTemplate>

 </Window.Resources>

   <Grid>
      <ListBox ItemsSource="{Binding Source={StaticResource someData}}" ItemTemplate="{DynamicResource BarChartItemsTemplate}" ItemsPanel="{DynamicResource BarChartItemsPanel}"/>

   </Grid>

</Window>

【问题讨论】:

    标签: wpf binding listbox datatemplate bar-chart


    【解决方案1】:

    您的 XAML 绑定到 ObjectProvider 创建的一个 DataFactory 实例,而您的代码隐藏完全创建另一个实例,UI 未绑定到该实例。

    试试这个让你开始。在您的 XAML 中,删除 ObjectProvider 并将您的 ListBox 更改为:

    <ListBox ItemsSource="{Binding}" ...
    

    dt_Tick 中,执行以下操作:

    this.DataContext = df.GetData();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      相关资源
      最近更新 更多