jdscodelab

File Manager Part-3 - CardButtons Custom Control

Jun 21st, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.45 KB | None | 0 0
  1. XAML:
  2.  
  3. <UserControl x:Class="FileManagerUI.Custom_Controls.CardButtons"
  4.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  7.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8.              xmlns:local="clr-namespace:FileManagerUI.Custom_Controls"
  9.              mc:Ignorable="d" x:Name="_cardButton"
  10.              d:DesignHeight="150" d:DesignWidth="250" MinHeight="150" MinWidth="250">
  11.     <UserControl.Resources>
  12.         <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
  13.             <Setter Property="Background" Value="White"/>
  14.             <Setter Property="BorderBrush" Value="#FF707070"/>
  15.             <Setter Property="Foreground" Value="DarkSlateGray"/>
  16.             <Setter Property="BorderThickness" Value="0"/>
  17.             <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
  18.             <Setter Property="VerticalContentAlignment" Value="Stretch"/>
  19.             <Setter Property="HorizontalAlignment" Value="Stretch"/>
  20.             <Setter Property="VerticalAlignment" Value="Stretch"/>
  21.             <Setter Property="Padding" Value="1"/>
  22.             <Setter Property="Focusable" Value="False"/>
  23.             <Setter Property="Template">
  24.                 <Setter.Value>
  25.                     <ControlTemplate TargetType="{x:Type Button}">
  26.                         <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="20" SnapsToDevicePixels="True">
  27.                             <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
  28.                         </Border>
  29.                         <ControlTemplate.Triggers>
  30.                             <Trigger Property="IsDefaulted" Value="True">
  31.                                 <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  32.                             </Trigger>
  33.                             <Trigger Property="IsMouseOver" Value="True">
  34.                                 <Setter Property="Background" TargetName="border" Value="#3BA2EA"/>
  35.                                 <Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/>
  36.                             </Trigger>
  37.                             <Trigger Property="IsPressed" Value="True">
  38.                                 <Setter Property="Background" TargetName="border" Value="#3BA2EA"/>
  39.                                 <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
  40.                             </Trigger>
  41.                             <Trigger Property="ToggleButton.IsChecked" Value="True">
  42.                                 <Setter Property="Background" TargetName="border" Value="#3BA2EA"/>
  43.                                 <Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/>
  44.                             </Trigger>
  45.                             <Trigger Property="IsEnabled" Value="False">
  46.                                 <Setter Property="Background" TargetName="border" Value="lightslategray"/>
  47.                                 <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
  48.                                 <Setter Property="Foreground" Value="#FF838383"/>
  49.                             </Trigger>
  50.                         </ControlTemplate.Triggers>
  51.                     </ControlTemplate>
  52.                 </Setter.Value>
  53.             </Setter>
  54.         </Style>
  55.         <Style x:Key="buttonIconBackground" TargetType="Border">
  56.             <Setter Property="Background" Value="{Binding ElementName=_cardButton, Path=IconBackground}"/>
  57.             <Style.Triggers>
  58.                 <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="True">
  59.                     <Setter Property="Background" Value="{Binding ElementName=_cardButton, Path=IconBackgroundMouseOver}"/>
  60.                 </DataTrigger>
  61.             </Style.Triggers>
  62.         </Style>
  63.         <Style x:Key="TextStyle1" TargetType="TextBlock">
  64.             <Setter Property="Margin" Value="25,0,0,0"/>
  65.             <Setter Property="FontWeight" Value="Bold"/>
  66.             <Setter Property="FontSize" Value="20"/>
  67.             <Setter Property="Foreground" Value="DarkSlateGray"/>
  68.             <Setter Property="HorizontalAlignment" Value="Left"/>
  69.             <Setter Property="VerticalAlignment" Value="Center"/>
  70.             <Style.Triggers>
  71.                 <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="true">
  72.                     <Setter Property="Foreground" Value="White"/>
  73.                 </DataTrigger>                
  74.             </Style.Triggers>
  75.         </Style>
  76.         <Style x:Key="TextStyle2" TargetType="TextBlock">
  77.             <Setter Property="Margin" Value="25,0,0,0"/>
  78.             <Setter Property="FontSize" Value="15"/>
  79.             <Setter Property="Foreground" Value="LightSlateGray"/>
  80.             <Setter Property="HorizontalAlignment" Value="Left"/>
  81.             <Setter Property="VerticalAlignment" Value="Center"/>
  82.             <Style.Triggers>
  83.                 <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="true">
  84.                     <Setter Property="Foreground" Value="White"/>
  85.                 </DataTrigger>
  86.             </Style.Triggers>
  87.         </Style>
  88.  
  89.         <Style x:Key="ProgressBarStyle" TargetType="local:CircularProgressBar">
  90.             <Setter Property="Margin" Value="0,0,0,22"/>
  91.             <Setter Property="HorizontalAlignment" Value="Center"/>
  92.             <Setter Property="VerticalAlignment" Value="Bottom"/>
  93.             <Setter Property="Height" Value="54"/>
  94.             <Setter Property="Width" Value="54"/>
  95.             <Setter Property="Value" Value="{Binding ElementName=_cardButton, Path=Progress}"/>
  96.             <Setter Property="FontSize" Value="{Binding ElementName=_cardButton, Path=FontSize}"/>
  97.             <Setter Property="ArcThickness" Value="{Binding ElementName=_cardButton, Path=ProgressBarThickness}"/>
  98.             <Setter Property="BackgroundBrush" Value="{Binding ElementName=_cardButton, Path=ProgressBackgroundBrush}"/>
  99.             <Setter Property="IndicatorBrush" Value="{Binding ElementName=_cardButton, Path=ProgressIndicatorBrush}"/>
  100.             <Setter Property="PFontColor" Value="{Binding ElementName=_cardButton,Path=ProgressTextColor}"/>
  101.             <Setter Property="PFontWeight" Value="{Binding ElementName=_cardButton,Path=ProgressTextWeight}"/>
  102.             <Style.Triggers>
  103.                 <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=Button, Mode=FindAncestor}}" Value="True">
  104.                     <Setter Property="BackgroundBrush" Value="{Binding ElementName=_cardButton, Path=ProgressBackgroundBrushMouseOver}"/>
  105.                     <Setter Property="IndicatorBrush" Value="{Binding ElementName=_cardButton, Path=ProgressIndicatorBrushMouseOver}"/>
  106.                     <Setter Property="PFontColor" Value="{Binding ElementName=_cardButton,Path=ProgressTextColorMouseOver}"/>
  107.                     <Setter Property="PFontWeight" Value="{Binding ElementName=_cardButton,Path=ProgressTextWeightMouseOver}"/>
  108.                 </DataTrigger>
  109.             </Style.Triggers>
  110.         </Style>
  111.     </UserControl.Resources>
  112.    
  113.     <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
  114.         <Button Style="{DynamicResource ButtonStyle}">
  115.             <Grid>
  116.                 <Grid.ColumnDefinitions>
  117.                     <ColumnDefinition Width="*"/>
  118.                     <ColumnDefinition Width="100"/>
  119.                 </Grid.ColumnDefinitions>
  120.                 <Grid.RowDefinitions>
  121.                     <RowDefinition Height="*"/>
  122.                     <RowDefinition Height="*"/>
  123.                 </Grid.RowDefinitions>
  124.                 <Border Grid.Column="0" Grid.Row="0" CornerRadius="10" HorizontalAlignment="Left" Margin="25,5,0,0" VerticalAlignment="Center" Height="35" Width="35" Style="{StaticResource buttonIconBackground}">
  125.                     <Image Width="20" Stretch="Uniform" Source="{Binding ElementName=_cardButton, Path=imageSource, TargetNullValue={x:Null}}"/>
  126.                 </Border>
  127.                 <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Vertical" VerticalAlignment="Stretch">
  128.                     <TextBlock Text="{Binding ElementName=_cardButton, Path=text1}" Style="{StaticResource TextStyle1}"/>
  129.                     <TextBlock Text="{Binding ElementName=_cardButton, Path=text2}" Style="{StaticResource TextStyle2}"/>
  130.                 </StackPanel>
  131.                 <local:CircularProgressBar Grid.Column="1" Grid.RowSpan="2"  Style="{StaticResource ProgressBarStyle}"/>
  132.             </Grid>
  133.         </Button>
  134.     </Grid>
  135. </UserControl>
  136.  
  137. ///Code Behind (.cs)
  138.  
  139. using System;
  140. using System.Windows;
  141. using System.Windows.Controls;
  142. using System.Windows.Media;
  143.  
  144. namespace FileManagerUI.Custom_Controls
  145. {
  146.     /// <summary>
  147.     /// Interaction logic for CardButtons.xaml
  148.     /// </summary>
  149.     public partial class CardButtons : UserControl
  150.     {
  151.         public CardButtons()
  152.         {
  153.             InitializeComponent();
  154.         }
  155.  
  156.  
  157.         public Uri imageSource
  158.         {
  159.             get { return (Uri)GetValue(imageSourceProperty); }
  160.             set { SetValue(imageSourceProperty, value); }
  161.         }
  162.  
  163.         // Using a DependencyProperty as the backing store for imageSource.  This enables animation, styling, binding, etc...
  164.         public static readonly DependencyProperty imageSourceProperty =
  165.             DependencyProperty.Register("imageSource", typeof(Uri), typeof(CardButtons));
  166.  
  167.  
  168.  
  169.         public string text1
  170.         {
  171.             get { return (string)GetValue(text1Property); }
  172.             set { SetValue(text1Property, value); }
  173.         }
  174.  
  175.         // Using a DependencyProperty as the backing store for text1.  This enables animation, styling, binding, etc...
  176.         public static readonly DependencyProperty text1Property =
  177.             DependencyProperty.Register("text1", typeof(string), typeof(CardButtons));
  178.  
  179.  
  180.  
  181.         public string text2
  182.         {
  183.             get { return (string)GetValue(text2Property); }
  184.             set { SetValue(text2Property, value); }
  185.         }
  186.  
  187.         // Using a DependencyProperty as the backing store for text2.  This enables animation, styling, binding, etc...
  188.         public static readonly DependencyProperty text2Property =
  189.             DependencyProperty.Register("text2", typeof(string), typeof(CardButtons));
  190.  
  191.         public SolidColorBrush IconBackground
  192.         {
  193.             get { return (SolidColorBrush)GetValue(IconBackgroundProperty); }
  194.             set { SetValue(IconBackgroundProperty, value); }
  195.         }
  196.  
  197.         // Using a DependencyProperty as the backing store for BackgroundBrush.  This enables animation, styling, binding, etc...
  198.         public static readonly DependencyProperty IconBackgroundProperty =
  199.             DependencyProperty.Register("IconBackground", typeof(SolidColorBrush), typeof(CardButtons));
  200.  
  201.         public SolidColorBrush IconBackgroundMouseOver
  202.         {
  203.             get { return (SolidColorBrush)GetValue(IconBackgroundMouseOverProperty); }
  204.             set { SetValue(IconBackgroundMouseOverProperty, value); }
  205.         }
  206.  
  207.         // Using a DependencyProperty as the backing store for IndicatorBrush.  This enables animation, styling, binding, etc...
  208.         public static readonly DependencyProperty IconBackgroundMouseOverProperty =
  209.             DependencyProperty.Register("IconBackgroundMouseOver", typeof(SolidColorBrush), typeof(CardButtons));
  210.  
  211.         public Brush ProgressTextColor
  212.         {
  213.             get { return (Brush)GetValue(ProgressTextProperty); }
  214.             set { SetValue(ProgressTextProperty, value); }
  215.         }
  216.  
  217.         // Using a DependencyProperty as the backing store for IndicatorBrush.  This enables animation, styling, binding, etc...
  218.         public static readonly DependencyProperty ProgressTextProperty =
  219.             DependencyProperty.Register("ProgressTextColor", typeof(Brush), typeof(CardButtons));
  220.  
  221.         public FontWeight ProgressTextWeight
  222.         {
  223.             get { return (FontWeight)GetValue(ProgressTextWeightProperty); }
  224.             set { SetValue(ProgressTextWeightProperty, value); }
  225.         }
  226.  
  227.         // Using a DependencyProperty as the backing store for BackgroundBrush.  This enables animation, styling, binding, etc...
  228.         public static readonly DependencyProperty ProgressTextWeightProperty =
  229.             DependencyProperty.Register("ProgressTextWeight", typeof(FontWeight), typeof(CardButtons));
  230.  
  231.         public Brush ProgressTextColorMouseOver
  232.         {
  233.             get { return (Brush)GetValue(ProgressTextColorMouseOverProperty); }
  234.             set { SetValue(ProgressTextColorMouseOverProperty, value); }
  235.         }
  236.  
  237.         // Using a DependencyProperty as the backing store for IndicatorBrush.  This enables animation, styling, binding, etc...
  238.         public static readonly DependencyProperty ProgressTextColorMouseOverProperty =
  239.             DependencyProperty.Register("ProgressTextColorMouseOver", typeof(Brush), typeof(CardButtons));
  240.  
  241.         public FontWeight ProgressTextWeightMouseOver
  242.         {
  243.             get { return (FontWeight)GetValue(ProgressTextWeightMouseOverProperty); }
  244.             set { SetValue(ProgressTextWeightMouseOverProperty, value); }
  245.         }
  246.  
  247.         // Using a DependencyProperty as the backing store for BackgroundBrush.  This enables animation, styling, binding, etc...
  248.         public static readonly DependencyProperty ProgressTextWeightMouseOverProperty =
  249.             DependencyProperty.Register("ProgressTextWeightMouseOver", typeof(FontWeight), typeof(CardButtons));
  250.  
  251.  
  252.         public int Progress
  253.         {
  254.             get { return (int)GetValue(ProgressProperty); }
  255.             set { SetValue(ProgressProperty, value); }
  256.         }
  257.         public int ProgressBarThickness
  258.         {
  259.             get { return (int)GetValue(ProgressBarThicknessProperty); }
  260.             set { SetValue(ProgressBarThicknessProperty, value); }
  261.         }
  262.  
  263.         // Using a DependencyProperty as the backing store for ArcThickness.  This enables animation, styling, binding, etc...
  264.         public static readonly DependencyProperty ProgressBarThicknessProperty =
  265.             DependencyProperty.Register("ProgressBarThickness", typeof(int), typeof(CardButtons));
  266.  
  267.         // Using a DependencyProperty as the backing store for Progress.  This enables animation, styling, binding, etc...
  268.         public static readonly DependencyProperty ProgressProperty =
  269.             DependencyProperty.Register("Progress", typeof(int), typeof(CardButtons));
  270.  
  271.         public Brush ProgressIndicatorBrush
  272.         {
  273.             get { return (Brush)GetValue(ProgressIndicatorBrushProperty); }
  274.             set { SetValue(ProgressIndicatorBrushProperty, value); }
  275.         }
  276.  
  277.         // Using a DependencyProperty as the backing store for IndicatorBrush.  This enables animation, styling, binding, etc...
  278.         public static readonly DependencyProperty ProgressIndicatorBrushProperty =
  279.             DependencyProperty.Register("ProgressIndicatorBrush", typeof(Brush), typeof(CardButtons));
  280.  
  281.         public Brush ProgressBackgroundBrush
  282.         {
  283.             get { return (Brush)GetValue(ProgressBackgroundBrushProperty); }
  284.             set { SetValue(ProgressBackgroundBrushProperty, value); }
  285.         }
  286.  
  287.         // Using a DependencyProperty as the backing store for BackgroundBrush.  This enables animation, styling, binding, etc...
  288.         public static readonly DependencyProperty ProgressBackgroundBrushProperty =
  289.             DependencyProperty.Register("ProgressBackgroundBrush", typeof(Brush), typeof(CardButtons));
  290.  
  291.         public Brush ProgressIndicatorBrushMouseOver
  292.         {
  293.             get { return (Brush)GetValue(ProgressIndicatorBrushMouseOverProperty); }
  294.             set { SetValue(ProgressIndicatorBrushMouseOverProperty, value); }
  295.         }
  296.  
  297.         // Using a DependencyProperty as the backing store for IndicatorBrush.  This enables animation, styling, binding, etc...
  298.         public static readonly DependencyProperty ProgressIndicatorBrushMouseOverProperty =
  299.             DependencyProperty.Register("ProgressIndicatorBrushMouseOver", typeof(Brush), typeof(CardButtons));
  300.  
  301.         public Brush ProgressBackgroundBrushMouseOver
  302.         {
  303.             get { return (Brush)GetValue(ProgressBackgroundBrushMouseOverProperty); }
  304.             set { SetValue(ProgressBackgroundBrushMouseOverProperty, value); }
  305.         }
  306.  
  307.         // Using a DependencyProperty as the backing store for BackgroundBrush.  This enables animation, styling, binding, etc...
  308.         public static readonly DependencyProperty ProgressBackgroundBrushMouseOverProperty =
  309.             DependencyProperty.Register("ProgressBackgroundBrushMouseOver", typeof(Brush), typeof(CardButtons));
  310.  
  311.     }
  312. }
Add Comment
Please, Sign In to add comment