5.2.1 WPF 通过ItemControl自己做柱状图

最终效果如下图:

1.1 准备数据 ViewModel

cs 复制代码
  public class PrimaryItemModel
  {
      public double Value { get; set; }
      public string XLabel { get; set; }
  }
 public class MainViewModel
 {
     public ObservableCollection<PrimaryItemModel> PrimaryList { get; set; }

     public MainViewModel()
     {
         Random random = new Random();
         PrimaryList = new ObservableCollection<PrimaryItemModel>();
         for (int i = 0; i < 15; i++)
         {
             PrimaryList.Add(new PrimaryItemModel
             {
                 XLabel = DateTime.Now.ToString("mm:ss"),
                 Value = random.Next(30, 200)
             });
         }
         Task.Run(async () =>
         {
             while (true)
             {
                 await Task.Delay(2000);
                 Application.Current.Dispatcher.Invoke(() =>
                 {
                     PrimaryList.Add(new PrimaryItemModel
                     {
                         XLabel = DateTime.Now.ToString("mm:ss"),
                         Value = random.Next(30, 200)

                     });
                     PrimaryList.RemoveAt(0);

                 });
             }
         });   
     }
 }
XML 复制代码
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="5*"/>
    </Grid.ColumnDefinitions>
    <ItemsControl Grid.Column="1" ItemsSource="{Binding PrimaryList}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="1"></UniformGrid>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="grid" Background="Transparent">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="30"/>
                    </Grid.RowDefinitions>
                    <StackPanel VerticalAlignment="Bottom">
                        <TextBlock Text="{Binding Value}" HorizontalAlignment="Center"/>
                        <Border Width="10" Height="{Binding Value}" Background="Orange" 
                                VerticalAlignment="Bottom" CornerRadius="5,5,0,0"/>
                    </StackPanel>
                    <Border BorderBrush="#DDD" BorderThickness="0,1,0,0" Grid.Row="1"/>
                    <TextBox Text="{Binding XLabel}" Grid.Row="1" VerticalAlignment="Center"
                             HorizontalAlignment="Center"/>
                </Grid>
                <DataTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="grid" Property="Background" Value="#EEE"/>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
           
        </ItemsControl.ItemTemplate>
        
    </ItemsControl>   
</Grid>
相关推荐
无所谓^_^1 分钟前
个人健康管理系统 | Springboot3 | Vue3
java·vue·毕业设计·springboot·课程设计·毕设·前后端分离
杨凯凡8 分钟前
Spring Boot 的 @Transactional 为什么会失效?从代理、自调用到异常回滚的排查实战
java·spring boot·spring·事务·后端开发
熊猫钓鱼>_>19 分钟前
三方库 NDK 原生模块跨平台适配与调试【鸿蒙心迹】
开发语言·华为·ai编程·harmonyos·鸿蒙·openharmony·鸿蒙心迹
曼岛_24 分钟前
[图解密码学]第5章 对称加密基础
java·数据库·密码学
HANDY0920025 分钟前
从“不照人”到“剂量达标”:扇叶遮挡式UV-C空气消毒的工程边界与可验证评估框架
开发语言·人工智能·嵌入式硬件·智能家居
浪扼飞舟1 小时前
WPF(控件的继承树、内容控件、布局控件)
wpf
1024奇点9 小时前
【仓颉语言入门 · 第二篇 第 22 课】
开发语言·ide·开源
Warren2Lynch10 小时前
C4 模型详解:利用 Visual Paradigm 可视化软件架构的综合指南
开发语言·uml
天河归来10 小时前
第三方接口不稳定,如何让核心任务继续完成
java·大数据
Knight_AL11 小时前
Java 文件下载时文件名中的 `#` 被转换成 `%23`?一次 URL 编码与解码问题排查
java·开发语言