时间:2021-07-01 10:21:17 帮助过:11人阅读
效果如图:大小随窗体自动调整。
2、堆叠
控件按指定方向排列,显示不下的时候根据所使用的控件不同会有区别。
<Grid ShowGridLines="True"> <Grid.RowDefinitions> <RowDefinition Height="60" /> <RowDefinition Height="60" /> <RowDefinition Height="60" /> <RowDefinition Height="60" /> <RowDefinition Height="60" /> <RowDefinition Height="60" /> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <TextBlock Text="statckpanelHorizontal 1" /> <TextBlock Text="statckpanelHorizontal 2" /> <TextBlock Text="statckpanelHorizontal 3" /> <TextBlock Text="statckpanelHorizontal 4" /> <TextBlock Text="statckpanelHorizontal 5" /> <TextBlock Text="statckpanelHorizontal 6" /> </StackPanel> <StackPanel Grid.Row="1" Orientation="Vertical"> <TextBlock Text="statckpanelVertical 1" /> <TextBlock Text="statckpanelVertical 2" /> <TextBlock Text="statckpanelVertical 3" /> <TextBlock Text="statckpanelVertical 4" /> <TextBlock Text="statckpanelVertical 5" /> <TextBlock Text="statckpanelVertical 6" /> </StackPanel> <VirtualizingStackPanel Grid.Row="2" Orientation="Horizontal"> <TextBlock Text="VirtualizingStackPanelHorizontal 1" /> <TextBlock Text="VirtualizingStackPanelHorizontal 2" /> <TextBlock Text="VirtualizingStackPanelHorizontal 3" /> <TextBlock Text="VirtualizingStackPanelHorizontal 4" /> <TextBlock Text="VirtualizingStackPanelHorizontal 5" /> <TextBlock Text="VirtualizingStackPanelHorizontal 6" /> </VirtualizingStackPanel> <VirtualizingStackPanel Grid.Row="3" Orientation="Vertical"> <TextBlock Text="VirtualizingStackPanelVertical 1" /> <TextBlock Text="VirtualizingStackPanelVertical 2" /> <TextBlock Text="VirtualizingStackPanelVertical 3" /> <TextBlock Text="VirtualizingStackPanelVertical 4" /> <TextBlock Text="VirtualizingStackPanelVertical 5" /> <TextBlock Text="VirtualizingStackPanelVertical 6" /> </VirtualizingStackPanel> <WrapPanel Grid.Row="4" Orientation="Horizontal"> <TextBlock Text="WrapPanelHorizontal 1" /> <TextBlock Text="WrapPanelHorizontal 2" /> <TextBlock Text="WrapPanelHorizontal 3" /> <TextBlock Text="WrapPanelHorizontal 4" /> <TextBlock Text="WrapPanelHorizontal 5" /> <TextBlock Text="WrapPanelHorizontal 6" /> </WrapPanel> <WrapPanel Grid.Row="5" Orientation="Vertical"> <TextBlock Text="WrapPanelVertical 1" /> <TextBlock Text="WrapPanelVertical 2" /> <TextBlock Text="WrapPanelVertical 3" /> <TextBlock Text="WrapPanelVertical 4" /> <TextBlock Text="WrapPanelVertical 5" /> <TextBlock Text="WrapPanelVertical 6" /> </WrapPanel> </Grid>View Code
效果如下:
StackPanel与VirtualizingStackPanel布局效果一样。WrapPanel比较特殊:指定水平方向堆叠时,水平放不下自动换成下一行显示;指定垂直方向堆叠时,垂直放不下时自动换成下一列显示。
3、停靠
DockPanel:子控件与面板的边缘对齐。这个分模块布局时用的最多。
<DockPanel> <WrapPanel Height="50" Background="LightBlue" DockPanel.Dock="Top" Orientation="Horizontal"> <TextBlock Text="Header----DockPanel.Dock=Top" /> <TextBlock Text="Header1" /> <TextBlock Text="Header1" /> <TextBlock Text="Header1" /> <TextBlock Text="Header1" /> <TextBlock Text="Header1" /> </WrapPanel> <WrapPanel Background="LightGray" DockPanel.Dock="Left" Orientation="Vertical"> <TextBlock Text="Left----DockPanel.Dock=Left" /> <TextBlock Text="menu1" /> <TextBlock Text="menu1" /> <TextBlock Text="menu1" /> <TextBlock Text="menu1" /> <TextBlock Text="menu1" /> <TextBlock Text="menu1" /> <TextBlock Text="menu1" /> <TextBlock Text="menu1" /> <TextBlock Text="menu1" /> </WrapPanel> <WrapPanel Background="LightSkyBlue" DockPanel.Dock="Right" Orientation="Horizontal"> <TextBlock Text="Right----DockPanel.Dock=Right" /> <TextBlock Text="content" /> <TextBlock Text="content" /> <TextBlock Text="content" /> <TextBlock Text="content" /> <TextBlock Text="content" /> <TextBlock Text="content" /> </WrapPanel> </DockPanel>View Code
效果如下:与Grid类似,不过是固定区域按方向自动延展。
二、式样与模板
通过定义式样和模板,重复使用它能让应用代码量下降,同时也方便以后维护修改。
1、式样 Style
<Window.Resources> <!-- 默认表示字体为16pt --> <Style TargetType="TextBlock"> <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="FontSize" Value="16" /> </Style> <!-- 继承默认设置并扩展 --> <Style x:Key="Title" BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="TextBlock"> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontSize" Value="20" /> </Style> </Window.Resources> <StackPanel Orientation="Vertical"> <TextBlock Text="default style" /> <TextBlock Style="{StaticResource Title}" Text="Title style" /> </StackPanel>View Code
自定义式样效果图:
2、模板 Template
WPF的所有控件都提供了默认的模板ControlTemplate,模板一般和式样结合使用。模板中需要特别学习VisualState和Trigger,通过VisualState可以设计在不同状态(鼠标按下,松开等)时控件显示样式,而通过Trigger可以让控件跟随参照对象数据或者绑定的数据做式样变化,内容比较多此篇不详细介绍。
如下是一个根据绑定的数据内容显示不同控件的例子。
<Window x:Class="WPFUI.Core.Template" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFUI.Core" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="Template" Width="800" Height="450" mc:Ignorable="d"> <Window.Resources> <!-- TextBox的显示模板 --> <DataTemplate x:Key="typeText" DataType="sys:String"> <TextBox Width="100" Text="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" /> </DataTemplate> <!-- Label的显示模板 --> <DataTemplate x:Key="typelabel" DataType="sys:String"> <Label Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" /> </DataTemplate> <!-- CheckBox的显示模板 --> <DataTemplate x:Key="typeCheck" DataType="sys:String"> <CheckBox Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" /> </DataTemplate> <!-- Image的显示模板 --> <DataTemplate x:Key="typeimg" DataType="sys:String"> <Label Background="LightBlue" Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" /> </DataTemplate> <DataTemplate x:Key="typeTemp"> <ContentPresenter x:Name="detail" Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" ContentTemplate="{StaticResource typeText}" /> <DataTemplate.Triggers> <!-- 如果字符为text,则显示TextBox模板 --> <DataTrigger Binding="{Binding}" Value="text"> <Setter TargetName="detail" Property="ContentTemplate" Value="{StaticResource typeText}" /> </DataTrigger> <!-- 如果字符为label,则显示Label模板 --> <DataTrigger Binding="{Binding}" Value="label"> <Setter TargetName="detail" Property="ContentTemplate" Value="{StaticResource typelabel}" /> </DataTrigger> <!-- 如果字符为check,则显示CheckBox模板 --> <DataTrigger Binding="{Binding}" Value="check"> <Setter TargetName="detail" Property="ContentTemplate" Value="{StaticResource typeCheck}" /> </DataTrigger> <!-- 如果字符为img,则显示Image模板 --> <DataTrigger Binding="{Binding}" Value="img"> <Setter TargetName="detail" Property="ContentTemplate" Value="{StaticResource typeimg}" /> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </Window.Resources> <Grid> <ListView ItemTemplate="{StaticResource typeTemp}"> <!-- 测试数据 --> <ListView.ItemsSource> <x:Array Type="sys:String"> <sys:String>text</sys:String> <sys:String>label</sys:String> <sys:String>check</sys:String> <sys:String>img</sys:String> </x:Array> </ListView.ItemsSource> <ListView.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Vertical" /> </ItemsPanelTemplate> </ListView.ItemsPanel> </ListView> </Grid> </Window>View Code
效果如下:
它是Google开源的一套UI设计系统,用它可以设计出非常美观的Web页面,App,桌面程序,尤其是图标非常全面。有它你不用懂Photoshop也可以设计出高端的界面。
官网地址:https://material.io/
一、引入MaterialDesign
WPF专用的MaterialDesign开源代码地址:https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit
项目介绍网站:http://materialdesigninxaml.net/
1、添加MaterialDesignThemes包
2、编辑App.xaml添加资源
全局引入MaterialDesign的资源文件
<prism:PrismApplication x:Class="WpfMaterial.Core.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfMaterial.Core" xmlns:prism="http://prismlibrary.com/"> <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.Indigo.xaml" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </prism:PrismApplication>View Code
3、配置MaterialDesign参照和字体
添加命名空间和字体(字体对图标显示很重要),这样就可以使用它里面的控件了。
<Window x:Class="WpfMaterial.Core.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:prism="http://prismlibrary.com/" Title="{Binding Title}" Width="525" Height="350" prism:ViewModelLocator.AutoWireViewModel="True" Background="{DynamicResource MaterialDesignPaper}" FontFamily="{DynamicResource MaterialDesignFont}"> <Grid> <materialDesign:Card Margin="16" Padding="32"> <TextBlock Style="{DynamicResource MaterialDesignTitleTextBlock}">My First Material Design App</TextBlock> </materialDesign:Card> </Grid> </Window>View Code
卡片效果:
二、使用MaterialDesign
场景一:登录界面
简单又美丽,图标都是MaterialDesign自带的。
相关代码:
<materialDesign:Card Width="425" Height="350" Margin="16" Padding="32"> <Grid> <Grid.RowDefinitions> <RowDefinition