C# WPF 一個設計界面

微信公衆號:Dotnet9,網站:Dotnet9,問題或建議:請網站留言
若是對您有所幫助:歡迎讚揚html

C# WPF 一個設計界面

今天正月初三,你們在家呆着挺好,不要忘了自我充電。git

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

閱讀導航express

  1. 本文背景
  2. 代碼實現
  3. 本文參考
  4. 源碼

1. 本文背景

一個不錯的界面設計c#

動畫

2. 代碼實現

使用 .NET Framework 4.8 建立名爲 「Dashboard1」 的WPF模板項目,添加3個Nuget庫:MaterialDesignThemes.3.1.0-ci98一、MaterialDesignColors.1.2.3-ci981和ModernUICharts.WPF.Beta.0.9.1,ModernUICharts 庫用於繪製統計圖,此庫沒有 .NET CORE 版本,因此項目是建立的 .NET Framework 版本。微信

解決方案主要文件目錄組織結構:app

  • Dashboard1
    • App.xaml
    • MainWindow.xaml
      • MainWindow.xaml.cs

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-namespace:De.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="PART_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="PageUp" Command="ScrollBar.PageDownCommand" Opacity="0" Focusable="false" />
                                    </Track.IncreaseRepeatButton>
                                    <Track.DecreaseRepeatButton>
                                        <RepeatButton x:Name="PageDown" 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="PART_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="PageDown" Property="Command" Value="ScrollBar.PageLeftCommand" />
                                    <Setter TargetName="PageUp" 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>
                                <materialDesign:PackIcon Kind="ContentCopy" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5" Foreground="White" Width="20" Height="20"/>
                            </Grid>
                            <TextBlock Text="使用空間" HorizontalAlignment="Right" FontFamily="Champagne &amp; Limousines" Margin="5" VerticalAlignment="Top" Foreground="Gray"/>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10 20">
                                <TextBlock Text="49/50" FontFamily="Champagne &amp; Limousines" VerticalAlignment="Center" Foreground="Gray" FontSize="50"/>
                                <TextBlock Text="GB" FontFamily="Champagne &amp; Limousines" Margin="0 5" Foreground="Gray" FontSize="20" VerticalAlignment="Bottom"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" Margin="20" Cursor="Hand">
                                <materialDesign:PackIcon 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>
                                <materialDesign:PackIcon Kind="Store" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5" Foreground="White" Width="20" Height="20"/>
                            </Grid>
                            <TextBlock Text="收入" HorizontalAlignment="Right" FontFamily="Champagne &amp; Limousines" Margin="5" VerticalAlignment="Top" Foreground="Gray"/>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10 30">
                                <TextBlock Text="¥" FontFamily="Champagne &amp; Limousines" Margin="0 2" Foreground="Gray" FontSize="20" VerticalAlignment="Bottom"/>
                                <TextBlock Text="35.674,00" FontFamily="Champagne &amp; Limousines" VerticalAlignment="Center" Foreground="Gray" FontSize="30"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" Margin="20">
                                <materialDesign:PackIcon 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>
                                <materialDesign:PackIcon Kind="InformationOutline" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5" Foreground="White" Width="20" Height="20"/>
                            </Grid>
                            <TextBlock Text="修正的錯誤" HorizontalAlignment="Right" FontFamily="Champagne &amp; Limousines" Margin="5" VerticalAlignment="Top" Foreground="Gray"/>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10 30">
                                <TextBlock Text="75" FontFamily="Champagne &amp; Limousines" VerticalAlignment="Center" Foreground="Gray" FontSize="40"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" Margin="20">
                                <materialDesign:PackIcon 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 &amp; Limousines" Foreground="Gray" FontSize="20"/>
                                <StackPanel Orientation="Horizontal" Margin="20 5">
                                    <materialDesign:PackIcon Kind="ArrowUp" Foreground="Green" VerticalAlignment="Center"/>
                                    <TextBlock Text="55%" FontFamily="Champagne &amp; Limousines" Foreground="Green" FontSize="15"/>
                                    <TextBlock Text="今天的銷售增加" Margin="20 0" FontFamily="Champagne &amp; Limousines" Foreground="Gray" FontSize="15"/>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="10 5">
                                    <materialDesign:PackIcon Kind="Clock" Foreground="Gray" VerticalAlignment="Center"/>
                                    <TextBlock Text="更新到4分鐘" Margin="5 0" FontFamily="Champagne &amp; 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="Percent" 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">
                        <materialDesign:PackIcon Kind="ViewDashboard" VerticalAlignment="Center"/>
                        <TextBlock HorizontalAlignment="Center" Text="儀表盤" FontFamily="Champagne &amp; Limousines"/>
                    </Grid>
                </Button>

                <Button Margin="10" Background="#FF8522BD" BorderBrush="#FF8522BD">
                    <Grid Width="150">
                        <materialDesign:PackIcon Kind="Account" VerticalAlignment="Center"/>
                        <TextBlock HorizontalAlignment="Center" Text="概況" FontFamily="Champagne &amp; 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 &amp; 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 &amp; 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 &amp; 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 &amp; 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.本文參考

  1. 視頻一:C# WPF Material Design UI: Dashboard,配套源碼:Dashboard1
  2. C# WPF開源控件庫《MaterialDesignInXAML》

4.源碼

演示代碼已所有貼上,另可參考原做者配套視頻及源碼學習,見【3.本文參考】

可運行Demo下載


除非註明,文章均由 Dotnet9 整理髮布,歡迎轉載。

轉載請註明本文地址:https://dotnet9.com/7806.html

歡迎掃描下方二維碼關注 Dotnet9 的微信公衆號,本站會及時推送最新技術文章

Dotnet9


時間如流水,只能流去不流回!

點擊《【閱讀原文】》,本站還有更多技術類文章等着您哦!!!

此刻順便爲我點個《【再看】》可好?

相關文章
相關標籤/搜索