本文章所介绍的WPF应用,坚持界面友好、美观的设计原则,主要模仿1password密码管理软件实现了一款密码管理、记事备忘这么一个功能。软件数据采用AES128加密方式存储于本地,确保数据安全。该软件实现了10个管理模板,和一个通用模板,来满足实际管理内容多样性的需要。其中包含的技术实现如下文罗列,实现的效果看下图。整个项目采用MvvM模式进行设计,大部分UI控件采用库的形式进行封装实现,方便控件的可重用。如对其中技术感兴趣可关注微博暗黑的光x进行留言,或发送邮件至1045354578@qq.com进行交流,优快云资源正在审核中,如需资源可留言。
软件UI主要包含的技术有:
1、自定义窗体的实现(带换肤功能,类似360),此实现网上很多,可自行搜索;
2、多种风格的自定义Button的实现;
3、带标签和图标的自定义RadioButton的实现;
4、带水印搜索栏的实现;
5、多种风格的自定义listbox的实现(带动画效果、不带动画效果都有);
6、listBox中的listviewitem排序、分组实现(按字母排序、按时间排序、按类排序);
7、自定义滚动条ScrollViewer的实现;
8、自定义进度条ProgressBar的实现;
9、自定义CheckBox的实现;
10、自定义TextBox的实现;
11、自定义Expander实现;
12、地址超链接功能;
13、AES128加密功能;
14、密码自动生成功能;
15、密码与密码明文切换功能;
16、排序功能切换功能;
17、文本加密读写功能;
18、关键字搜索、拼音搜索功能。
界面
换肤
新建管理项
密码自动生成配置
图标修改
以下代码为部分自定义ListBox代码
<!--GroupItem样式-->
<Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Border BorderBrush="Silver" BorderThickness="0,1,0,1">
<TextBlock Text="{Binding Name}" Margin="13,0,0,0"/>
</Border>
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--GroupItem样式-->
<Style x:Key="ExpanderContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<PControl:PExpander Header="{Binding Name}">
<ItemsPresenter />
</PControl:PExpander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--密码风格ListBoxItem-->
<Style x:Key="PsdListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border>
<Grid x:Name="templateRoot" SnapsToDevicePixels="True" Height="45" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="55"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border x:Name="mainBorder" BorderThickness="0,1,0,1" Margin="0" Grid.ColumnSpan="1"
Background="{TemplateBinding Background}" HorizontalAlignment="Center"
VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5"/>
<Image Source="{Binding IconPath}" Margin="10,5,10,5" Grid.Column="0"/>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="26"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="contentPresenter" VerticalAlignment="Bottom" Text="{Binding Title}"
Foreground="{TemplateBinding Foreground}"/>
<TextBlock Grid.Row="1" Text="{Binding Describe}" Foreground="{StaticResource WaterGrayBrush}"
FontSize="11" VerticalAlignment="Top"/>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="templateRoot" Value="#FFC1C4C7"/>
</Trigger>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter Property="Background" TargetName="templateRoot" Value="#FFF11565"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--ListBox风格-->
<Style x:Key="ListBoxStyle" TargetType="ListBox">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Border Name="Bd" SnapsToDevicePixels="True"
BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="1,1,1,1" BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}">
<PControl:PScrollViewer Focusable="False" Padding="{TemplateBinding Control.Padding}"
ScrollViewer.CanContentScroll="False">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
</PControl:PScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--GeneralCheckListBoxItemStyle-->
<Style x:Key="GeneralCheckListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<StackPanel Orientation="Horizontal" Margin="0,4,0,4">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}"
Foreground="Gray" TextAlignment="Right"
FontFamily="微软雅黑" FontSize="13"
Width="100" TextWrapping="Wrap"
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"/>
<TextBox Text="{Binding Content}" Margin="20,0,0,0">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontFamily" Value="微软雅黑"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="Width" Value="250"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="AcceptsReturn" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid>
<ScrollViewer x:Name="PART_ContentHost" Focusable="false"
Background="Transparent"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TextBox.Style>
</TextBox>
</StackPanel>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--GeneralListBoxItemStyle-->
<Style x:Key="GeneralListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<StackPanel Orientation="Horizontal" Margin="0,8,0,8">
<TextBlock Text="{Binding Title}" Foreground="Gray" FontFamily="微软雅黑"
FontSize="14" VerticalAlignment="Center" Width="80" TextAlignment="Right"/>
<PControl:PIconButton Style="{StaticResource SignalButton}" Margin="0,0,10,0"
Command="{Binding DataContext.EiditTitleCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}}"
CommandParameter="{Binding DataContext,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}"/>
<TextBox Text="{Binding Content,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DefaultTextBox}"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--GeneralListBoxStyle-->
<Style x:Key="GeneralListBoxStyle" TargetType="ListBox">
<Setter Property="Height" Value="Auto"/>
<Setter Property="Width" Value="Auto"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center"
IsItemsHost="True" VerticalAlignment="Top"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Border BorderThickness="0">
<ScrollViewer Focusable="False">
<ItemsPresenter Margin="0"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--大图标风格ListBoxItem-->
<Style x:Key="IconListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Margin" Value="5,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Width="60">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border BorderThickness="2"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="Transparent"
Background="{TemplateBinding Panel.Background}"
Name="Bd" SnapsToDevicePixels="True"
CornerRadius="6" Grid.RowSpan="1">
</Border>
<Image Height="48" Margin="6,6" Source="{Binding Path=Path}"/>
<TextBlock TextAlignment="Center" HorizontalAlignment="Center"
Grid.Row="1" Text="{Binding Path=Name}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Bd" Property="BorderBrush" Value="Gray"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="False" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd" Property="BorderBrush" Value="Silver"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--大图标风格ListBox-->
<Style x:Key="IconListBoxStyle" TargetType="ListBox">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource IconListBoxItemStyle}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Border Name="Border" BorderThickness="0">
<PControl:PScrollViewer Focusable="True"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"
CanContentScroll="False">
<WrapPanel IsItemsHost="True"/>
</PControl:PScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
自定义TextBox代码
<!--TextBox-->
<Style x:Key="DefaultTextBox" TargetType="TextBox">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontFamily" Value="微软雅黑"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="MinHeight" Value="28"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="Width" Value="300"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="AcceptsReturn" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid x:Name="main_grid">
<Border x:Name="main_border" BorderThickness="{TemplateBinding BorderThickness}"
Background="White" CornerRadius="5">
<Border x:Name="exmain_border" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{StaticResource NormalBlueBrush}" CornerRadius="4">
<ScrollViewer x:Name="PART_ContentHost" Margin="3,0,3,0"
Focusable="false"
Background="White"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
</Border>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="main_grid" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="main_border" Value="{StaticResource MouseOverBlueBrush}"/>
<Setter Property="BorderBrush" TargetName="exmain_border" Value="{StaticResource ExMouseOverBlueBrush}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsKeyboardFocused" Value="True"/>
<Condition Property="IsMouseOver" Value="true"/>
</MultiTrigger.Conditions>
<Setter Property="BorderBrush" TargetName="main_border" Value="{StaticResource MouseOverBlueBrush}"/>
<Setter Property="BorderBrush" TargetName="exmain_border" Value="{StaticResource ExMouseOverBlueBrush}"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--TextBox-->
<Style x:Key="ReadOnlyTextBox" TargetType="TextBox">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontFamily" Value="微软雅黑"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="MaxWidth" Value="300"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="AcceptsReturn" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid>
<ScrollViewer x:Name="PART_ContentHost" Focusable="false"
Background="Transparent"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--PasswordBox-->
<Style x:Key="DefaultPasswordBox" TargetType="PasswordBox">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontFamily" Value="微软雅黑"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="MinHeight" Value="28"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="PasswordBox">
<Grid x:Name="main_grid">
<Border x:Name="main_border" BorderThickness="{TemplateBinding BorderThickness}"
Background="White" CornerRadius="5">
<Border x:Name="exmain_border" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{StaticResource NormalBlueBrush}" CornerRadius="4">
<ScrollViewer x:Name="PART_ContentHost" Margin="3,0,3,0"
Focusable="false"
Background="White"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
</Border>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="main_grid" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="main_border" Value="{StaticResource MouseOverBlueBrush}"/>
<Setter Property="BorderBrush" TargetName="exmain_border" Value="{StaticResource ExMouseOverBlueBrush}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsKeyboardFocused" Value="True"/>
<Condition Property="IsMouseOver" Value="true"/>
</MultiTrigger.Conditions>
<Setter Property="BorderBrush" TargetName="main_border" Value="{StaticResource MouseOverBlueBrush}"/>
<Setter Property="BorderBrush" TargetName="exmain_border" Value="{StaticResource ExMouseOverBlueBrush}"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>