找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3656|回复: 12

C# WPF 一个设计界面

 火.. [复制链接]
  • 打卡等级:常驻代表
  • 打卡总天数:34
  • 打卡月天数:6
  • 打卡总奖励:9027
  • 最近打卡:2025-12-17 23:15:51

2823

主题

541

回帖

2万

积分

管理员

积分
22569
发表于 2020-5-25 23:14:04 | 显示全部楼层 |阅读模式
C# WPF 一个设计界面

今天正月初三,大家在家呆着挺好,不要忘了自我充电。

武汉人民加油,今早又有噩耗,24号(8号)一路走好。

阅读导航

  • 本文背景
  • 代码实现
  • 本文参考
  • 源码
1. 本文背景

一个不错的界面设计



2. 代码实现

使用 .NET Framework 4.8 创建名为 “Dashboard1” 的WPF模板项目,添加3个Nuget库:MaterialDesignThemes.3.1.0-ci981、MaterialDesignColors.1.2.3-ci981和ModernUICharts.WPF.Beta.0.9.1,ModernUICharts 库用于绘制统计图,此库没有 .NET CORE 版本,所以项目是创建的 .NET Framework 版本。

解决方案主要文件目录组织结构:

  • Dashboard1
    • MainWindow.xaml.cs
    • App.xaml
    • MainWindow.xaml
2.1 引入样式

文件【App.xaml】,在 StartupUri 中设置启动的视图【MainWindow.xaml】,并在【Application.Resources】节点增加 MaterialDesignThemes库的样式文件:

<Application.Resources>    <ResourceDictionary>        <ResourceDictionary.MergedDictionaries>            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml" />        </ResourceDictionary.MergedDictionaries>    </ResourceDictionary></Application.Resources>2.2 演示窗体

文件【MainWindow.xaml】,布局代码、统计图MVVM绑定代码都在此文件中,源码如下:

<Window x:Class="Dashboard1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"        xmlns:MetroChart="clr-namespacee.TorstenMandelkow.MetroChart;assembly=De.TorstenMandelkow.MetroChart"        mc:Ignorable="d" Height="600" Width="1024" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">    <Window.Resources>        <ResourceDictionary>            <Style x:Key="ScrollThumbs" TargetType="{x:Type Thumb}">                <Setter Property="Template">                    <Setter.Value>                        <ControlTemplate TargetType="{x:Type Thumb}">                            <Grid x:Name="Grid">                                <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Fill="Transparent" />                                <Border x:Name="Rectangle1" CornerRadius="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto"  Background="{TemplateBinding Background}" />                            </Grid>                            <ControlTemplate.Triggers>                                <Trigger Property="Tag" Value="Horizontal">                                    <Setter TargetName="Rectangle1" Property="Width" Value="Auto" />                                    <Setter TargetName="Rectangle1" Property="Height" Value="7" />                                </Trigger>                            </ControlTemplate.Triggers>                        </ControlTemplate>                    </Setter.Value>                </Setter>            </Style>            <!--ScrollBars-->            <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">                <Setter Property="Stylus.IsFlicksEnabled" Value="false" />                <Setter Property="Foreground" Value="LightGray" />                <Setter Property="Background" Value="DarkGray" />                <Setter Property="Width" Value="10" />                <Setter Property="Template">                    <Setter.Value>                        <ControlTemplate TargetType="{x:Type ScrollBar}">                            <Grid x:Name="GridRoot" Width="19" Background="{x:Null}">                                <Track x:Name="ART_Track" Grid.Row="0" IsDirectionReversed="true" Focusable="false">                                    <Track.Thumb>                                        <Thumb x:Name="Thumb" Background="{TemplateBinding Foreground}" Style="{DynamicResource ScrollThumbs}" />                                    </Track.Thumb>                                    <Track.IncreaseRepeatButton>                                        <RepeatButton x:Name="ageUp" Command="ScrollBar.PageDownCommand" Opacity="0" Focusable="false" />                                    </Track.IncreaseRepeatButton>                                    <Track.DecreaseRepeatButton>                                        <RepeatButton x:Name="ageDown" Command="ScrollBar.PageUpCommand" Opacity="0" Focusable="false" />                                    </Track.DecreaseRepeatButton>                                </Track>                            </Grid>                            <ControlTemplate.Triggers>                                <Trigger SourceName="Thumb" Property="IsMouseOver" Value="true">                                    <Setter Value="{DynamicResource ButtonSelectBrush}" TargetName="Thumb" Property="Background" />                                </Trigger>                                <Trigger SourceName="Thumb" Property="IsDragging" Value="true">                                    <Setter Value="{DynamicResource DarkBrush}" TargetName="Thumb" Property="Background" />                                </Trigger>                                <Trigger Property="IsEnabled" Value="false">                                    <Setter TargetName="Thumb" Property="Visibility" Value="Collapsed" />                                </Trigger>                                <Trigger Property="Orientation" Value="Horizontal">                                    <Setter TargetName="GridRoot" Property="LayoutTransform">                                        <Setter.Value>                                            <RotateTransform Angle="-90" />                                        </Setter.Value>                                    </Setter>                                    <Setter TargetName="ART_Track" Property="LayoutTransform">                                        <Setter.Value>                                            <RotateTransform Angle="-90" />                                        </Setter.Value>                                    </Setter>                                    <Setter Property="Width" Value="Auto" />                                    <Setter Property="Height" Value="12" />                                    <Setter TargetName="Thumb" Property="Tag" Value="Horizontal" />                                    <Setter TargetName="ageDown" Property="Command" Value="ScrollBar.PageLeftCommand" />                                    <Setter TargetName="ageUp" Property="Command" Value="ScrollBar.PageRightCommand" />                                </Trigger>                            </ControlTemplate.Triggers>                        </ControlTemplate>                    </Setter.Value>                </Setter>            </Style>        </ResourceDictionary>    </Window.Resources>    <Grid>        <Grid.ColumnDefinitions>            <ColumnDefinition Width="200"/>            <ColumnDefinition Width="*"/>        </Grid.ColumnDefinitions>        <Grid.RowDefinitions>            <RowDefinition Height="30"/>            <RowDefinition Height="*"/>        </Grid.RowDefinitions>        <Grid Grid.Column="1" Grid.Row="1" Background="#FFCFCFCF">            <ScrollViewer>                <Grid>                    <Grid.RowDefinitions>                        <RowDefinition Height="200"/>                        <RowDefinition Height="*"/>                        <RowDefinition Height="*"/>                    </Grid.RowDefinitions>                    <Grid.ColumnDefinitions>                        <ColumnDefinition Width="1*"/>                        <ColumnDefinition Width="1*"/>                        <ColumnDefinition Width="1*"/>                    </Grid.ColumnDefinitions>                    <Grid Grid.Column="0">                        <Rectangle Height="120" Margin="20" Fill="White" RadiusY="10" RadiusX="10" >                            <Rectangle.Effect>                                <DropShadowEffect BlurRadius="20" Color="#FFDEDEDE" RenderingBias="Quality" ShadowDepth="1"/>                            </Rectangle.Effect>                        </Rectangle>                        <Grid Margin="25" Height="120">                            <Grid Width="35" Height="50" Background="#FFFFAF24" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20 0">                                <Grid.Effect>                                    <DropShadowEffect BlurRadius="20" Color="#FFECECEC" RenderingBias="Quality" ShadowDepth="1"/>                                </Grid.Effect>                                <materialDesignackIcon Kind="ContentCopy" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5" Foreground="White" Width="20" Height="20"/>                            </Grid>                            <TextBlock Text="使用空间" HorizontalAlignment="Right" FontFamily="Champagne & Limousines" Margin="5" VerticalAlignment="Top" Foreground="Gray"/>                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10 20">                                <TextBlock Text="49/50" FontFamily="Champagne & Limousines" VerticalAlignment="Center" Foreground="Gray" FontSize="50"/>                                <TextBlock Text="GB" FontFamily="Champagne & Limousines" Margin="0 5" Foreground="Gray" FontSize="20" VerticalAlignment="Bottom"/>                            </StackPanel>                            <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" Margin="20" Cursor="Hand">                                <materialDesignackIcon Kind="AlertOutline" Foreground="Red" Width="10" Height="10" VerticalAlignment="Center" Margin="5 0"/>                                <TextBlock Text="获得更多空间" FontSize="8" Foreground="#FF8522BD"/>                            </StackPanel>                        </Grid>                    </Grid>                    <Grid Grid.Column="1">                        <Rectangle Height="120" Margin="20" Fill="White" RadiusY="10" RadiusX="10" >                            <Rectangle.Effect>                                <DropShadowEffect BlurRadius="20" Color="#FFDEDEDE" RenderingBias="Quality" ShadowDepth="1"/>                            </Rectangle.Effect>                        </Rectangle>                        <Grid Margin="25" Height="120">                            <Grid Width="35" Height="50" Background="#FF41A43C" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20 0">                                <Grid.Effect>                                    <DropShadowEffect BlurRadius="20" Color="#FFECECEC" RenderingBias="Quality" ShadowDepth="1"/>                                </Grid.Effect>                                <materialDesignackIcon Kind="Store" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5" Foreground="White" Width="20" Height="20"/>                            </Grid>                            <TextBlock Text="收入" HorizontalAlignment="Right" FontFamily="Champagne & Limousines" Margin="5" VerticalAlignment="Top" Foreground="Gray"/>                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10 30">                                <TextBlock Text="¥" FontFamily="Champagne & Limousines" Margin="0 2" Foreground="Gray" FontSize="20" VerticalAlignment="Bottom"/>                                <TextBlock Text="35.674,00" FontFamily="Champagne & Limousines" VerticalAlignment="Center" Foreground="Gray" FontSize="30"/>                            </StackPanel>                            <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" Margin="20">                                <materialDesignackIcon Kind="Calendar" Foreground="Gray" Width="10" Height="10" VerticalAlignment="Center" Margin="5 0"/>                                <TextBlock Text="最近24小时" FontSize="8" Foreground="Gray"/>                            </StackPanel>                        </Grid>                    </Grid>                    <Grid Grid.Column="2">                        <Rectangle Height="120" Margin="20" Fill="White" RadiusY="10" RadiusX="10" >                            <Rectangle.Effect>                                <DropShadowEffect BlurRadius="20" Color="#FFDEDEDE" RenderingBias="Quality" ShadowDepth="1"/>                            </Rectangle.Effect>                        </Rectangle>                        <Grid Margin="25" Height="120">                            <Grid Width="35" Height="50" Background="#FFCF1F1F" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20 0">                                <Grid.Effect>                                    <DropShadowEffect BlurRadius="20" Color="#FFECECEC" RenderingBias="Quality" ShadowDepth="1"/>                                </Grid.Effect>                                <materialDesignackIcon Kind="InformationOutline" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5" Foreground="White" Width="20" Height="20"/>                            </Grid>                            <TextBlock Text="修正的错误" HorizontalAlignment="Right" FontFamily="Champagne & Limousines" Margin="5" VerticalAlignment="Top" Foreground="Gray"/>                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10 30">                                <TextBlock Text="75" FontFamily="Champagne & Limousines" VerticalAlignment="Center" Foreground="Gray" FontSize="40"/>                            </StackPanel>                            <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" Margin="20">                                <materialDesignackIcon Kind="GithubCircle" Foreground="Gray" Width="10" Height="10" VerticalAlignment="Center" Margin="5 0"/>                                <TextBlock Text="Github" FontSize="8" Foreground="Gray"/>                            </StackPanel>                        </Grid>                    </Grid>                    <Grid Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="3" HorizontalAlignment="Center" Width="580" Height="510">                        <Grid Background="White" Margin="20 50 20 20">                            <Grid.OpacityMask>                                <VisualBrush Visual="{ Binding ElementName=BorderG1 }"/>                            </Grid.OpacityMask>                            <Border x:Name="BorderG1" CornerRadius="5" Background="White"/>                            <StackPanel VerticalAlignment="Bottom" >                                <TextBlock Text="日收入" Margin="10 0" FontFamily="Champagne & Limousines" Foreground="Gray" FontSize="20"/>                                <StackPanel Orientation="Horizontal" Margin="20 5">                                    <materialDesignackIcon Kind="ArrowUp" Foreground="Green" VerticalAlignment="Center"/>                                    <TextBlock Text="55%" FontFamily="Champagne & Limousines" Foreground="Green" FontSize="15"/>                                    <TextBlock Text="今天的销售增长" Margin="20 0" FontFamily="Champagne & Limousines" Foreground="Gray" FontSize="15"/>                                </StackPanel>                                <StackPanel Orientation="Horizontal" Margin="10 5">                                    <materialDesignackIcon Kind="Clock" Foreground="Gray" VerticalAlignment="Center"/>                                    <TextBlock Text="更新到4分钟" Margin="5 0" FontFamily="Champagne & Limousines" Foreground="Gray" FontSize="15"/>                                </StackPanel>                            </StackPanel>                        </Grid>                        <Grid Margin="50 20 50 150">                            <Grid.OpacityMask>                                <VisualBrush Visual="{ Binding ElementName=BorderG2 }"/>                            </Grid.OpacityMask>                            <Border x:Name="BorderG2" CornerRadius="15" Background="#FF340051"/>                            <MetroChart:RadialGaugeChart Background="{x:Null}" ChartTitle="Consumo" ChartSubTitle="" Foreground="LightGray" >                                <MetroChart:RadialGaugeChart.Series>                                    <MetroChart:ChartSeries                                        DisplayMember="Title"                                        ItemsSource="{Binding Path=Consumo}"                                        SeriesTitle="Consumo"                                        ValueMember="ercent" HorizontalAlignment="Center"/>                                </MetroChart:RadialGaugeChart.Series>                            </MetroChart:RadialGaugeChart>                        </Grid>                    </Grid>                </Grid>            </ScrollViewer>        </Grid>        <Grid Grid.Row="1">            <Grid.Background>                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">                    <GradientStop Color="LightGray" Offset="1"/>                    <GradientStop Color="#FFE6E6E6"/>                </LinearGradientBrush>            </Grid.Background>            <Grid.RowDefinitions>                <RowDefinition Height="100"/>                <RowDefinition Height="*"/>            </Grid.RowDefinitions>            <Grid.Effect>                <DropShadowEffect BlurRadius="20" Color="#FFDEDEDE" RenderingBias="Quality" ShadowDepth="1"/>            </Grid.Effect>            <Grid Background="#FFA46FE4">                <Image Source="https://img.dotnet9.com/logo-head.png"/>            </Grid>            <StackPanel Grid.Row="1">                <Button Margin="10" Background="#FF8522BD" BorderBrush="#FF8522BD">                    <Grid Width="150">                        <materialDesignackIcon Kind="ViewDashboard" VerticalAlignment="Center"/>                        <TextBlock HorizontalAlignment="Center" Text="仪表盘" FontFamily="Champagne & Limousines"/>                    </Grid>                </Button>                <Button Margin="10" Background="#FF8522BD" BorderBrush="#FF8522BD">                    <Grid Width="150">                        <materialDesignackIcon Kind="Account" VerticalAlignment="Center"/>                        <TextBlock HorizontalAlignment="Center" Text="概况" FontFamily="Champagne & Limousines"/>                    </Grid>                </Button>                <Button Margin="10" Background="#FF8522BD" BorderBrush="#FF8522BD">                    <Grid Width="150">                        <materialDesign:PackIcon Kind="ContentPaste" VerticalAlignment="Center"/>                        <TextBlock HorizontalAlignment="Center" Text="表格" FontFamily="Champagne & Limousines"/>                    </Grid>                </Button>                <Button Margin="10" Background="#FF8522BD" BorderBrush="#FF8522BD">                    <Grid Width="150">                        <materialDesign:PackIcon Kind="TshirtCrew" VerticalAlignment="Center"/>                        <TextBlock HorizontalAlignment="Center" Text="产品" FontFamily="Champagne & Limousines"/>                    </Grid>                </Button>                <Button Margin="10" Background="#FF8522BD" BorderBrush="#FF8522BD">                    <Grid Width="150">                        <materialDesign:PackIcon Kind="TruckDelivery" VerticalAlignment="Center"/>                        <TextBlock HorizontalAlignment="Center" Text="供应商" FontFamily="Champagne & Limousines"/>                    </Grid>                </Button>                <Button Margin="10" Background="#FF8522BD" BorderBrush="#FF8522BD">                    <Grid Width="150">                        <materialDesign:PackIcon Kind="Settings" VerticalAlignment="Center"/>                        <TextBlock HorizontalAlignment="Center" Text="配置" FontFamily="Champagne & Limousines"/>                    </Grid>                </Button>            </StackPanel>        </Grid>        <Grid x:Name="GridBarraTitle" Grid.ColumnSpan="2" Background="#FF8522BD" MouseDown="GridBarraTitle_MouseDown">            <TextBlock Text="仪表盘演示标题(https://dotnet9.com)" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="17"/>            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="10,0">                <Button Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Width="25" Height="25" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="White">                    <materialDesign:PackIcon Kind="Bell"/>                </Button>                <Button Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Width="25" Height="25" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="White">                    <materialDesign:PackIcon Kind="Account"/>                </Button>                <Button x:Name="ButtonFechar" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Width="25" Height="25" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="White" Click="ButtonFechar_Click">                    <materialDesign:PackIcon Kind="Close"/>                </Button>            </StackPanel>        </Grid>    </Grid></Window>

窗口布局代码也不多,就是布局和数据绑定,下面是后台代码:文件【MainWindow.xaml.cs】,ViewModel绑定、关闭窗体、窗体移动等事件处理,因为是演示事例,所以写的简单。

using System.Collections.Generic;using System.Windows;using System.Windows.Input;namespace Dashboard1{    /// <summary>    /// MainWindow.xaml的逻辑交互    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            Consumo consumo = new Consumo();            DataContext = new ConsumoViewModel(consumo);        }        private void ButtonFechar_Click(object sender, RoutedEventArgs e)        {            Application.Current.Shutdown();        }        private void GridBarraTitle_MouseDown(object sender, MouseButtonEventArgs e)        {            DragMove();        }    }    internal class ConsumoViewModel    {        public List<Consumo> Consumo { get; private set; }        public ConsumoViewModel(Consumo consumo)        {            Consumo = new List<Consumo>();            Consumo.Add(consumo);        }    }    internal class Consumo    {        public string Title { get; private set; }        public int Percent { get; private set; }        public Consumo()        {            Title = "电量消耗";            Percent = CalcularPercent();        }        private int CalcularPercent()        {            return 47; //消费百分比的计算        }    }}3.本文参考
  • 视频一:C# WPF Material Design UI: Dashboard,配套源码:Dashboard1。
  • C# WPF开源控件库《MaterialDesignInXAML》

















工控课堂 www.gkket.com

0

主题

138

回帖

420

积分

注册会员

积分
420
发表于 2020-5-25 23:14:04 | 显示全部楼层
绝对干货,楼主给力,支持了!!!
工控课堂 www.gkket.com

0

主题

276

回帖

840

积分

中级会员

积分
840
发表于 2020-10-24 12:09:43 | 显示全部楼层
真是被感动的痛哭流涕……
工控课堂 www.gkket.com

0

主题

95

回帖

158

积分

新手上路

积分
158
发表于 2025-11-23 11:23:54 | 显示全部楼层
这评论区卧虎藏龙,个个都是人才!
工控课堂 www.gkket.com

0

主题

53

回帖

98

积分

新手上路

积分
98
发表于 2025-11-23 11:29:29 | 显示全部楼层
不请自来,只为说一句:太精彩了!
工控课堂 www.gkket.com

0

主题

90

回帖

138

积分

新手上路

积分
138
发表于 2025-11-23 11:30:48 | 显示全部楼层
赞同 + 10086,完全说出了我的想法!
工控课堂 www.gkket.com

0

主题

86

回帖

120

积分

新手上路

积分
120
发表于 2025-11-23 11:35:35 | 显示全部楼层
来凑个热闹,为楼主增加点人气!
工控课堂 www.gkket.com

0

主题

52

回帖

64

积分

新手上路

积分
64
发表于 2025-11-23 11:36:45 | 显示全部楼层
水个经验,楼主加油,支持你~
工控课堂 www.gkket.com

0

主题

85

回帖

121

积分

新手上路

积分
121
发表于 2025-11-23 11:36:59 | 显示全部楼层
这逻辑绝了,分析得太到位了吧
工控课堂 www.gkket.com

0

主题

87

回帖

144

积分

新手上路

积分
144
发表于 2025-11-23 11:39:09 | 显示全部楼层
占个楼慢慢看,先马克一下
楼主辛苦啦,期待下一篇分享
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|手机版|免责声明|本站介绍|工控课堂 ( 沪ICP备20008691号-1 )

GMT+8, 2025-12-23 08:55 , Processed in 0.129575 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表