Guest User

WPF #1

a guest
Mar 25th, 2025
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.70 KB | None | 0 0
  1. // Nav bar:
  2.  
  3. Xaml:
  4.  
  5. <UserControl x:Class="BankApp.Components.NavPanel"
  6.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  7.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  8.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  10.              xmlns:local="clr-namespace:BankApp.Components"
  11.              mc:Ignorable="d"
  12.              d:DesignHeight="450" d:DesignWidth="800">
  13.  
  14.     <Grid
  15.         VerticalAlignment="Top"
  16.         Height="34"
  17.         Background="White"
  18.         Margin="1"
  19.     >
  20.  
  21.         <Grid.ColumnDefinitions>
  22.             <ColumnDefinition Width="*" />
  23.             <ColumnDefinition Width="Auto" />
  24.         </Grid.ColumnDefinitions>
  25.  
  26.         <!--This takes care of the buttons on the left-->
  27.         <StackPanel
  28.                 VerticalAlignment="Top"
  29.                 Height="34"
  30.                 Background="White"
  31.                 Grid.Column="0"
  32.                 Orientation="Horizontal"
  33.         >
  34.  
  35.             <local:NavButton
  36.                 x:Name="btnHome"
  37.                 type="HOME"
  38.                 label="Home"
  39.             />
  40.        
  41.             <local:NavButton
  42.                 x:Name="btnTransfer"
  43.                 type="TRANSFER"
  44.                 label="Transfer"
  45.             />
  46.        
  47.             <local:NavButton
  48.                 x:Name="btnWithdraw"
  49.                 type="WITHDRAW"
  50.                 label="Withdraw"
  51.             />
  52.  
  53.             <local:NavButton
  54.                 x:Name="btnView"
  55.                 type="VIEW"
  56.                 label="View Statement"
  57.             />
  58.  
  59.         </StackPanel>
  60.  
  61.         <!--This takes care of the buttons on the right-->
  62.         <StackPanel
  63.                 VerticalAlignment="Top"
  64.                 Height="34"
  65.                 Background="White"
  66.                 Grid.Column="1"
  67.                 Orientation="Horizontal"
  68.         >
  69.            
  70.             <local:NavButton
  71.                 x:Name="btnLogin"
  72.                 type="LOGIN"
  73.                 label="Login"
  74.                 Grid.Column="1"
  75.             />
  76.         </StackPanel>
  77.  
  78.  
  79.     </Grid>
  80. </UserControl>
  81.  
  82. // ============== Nav Buttons =====================
  83. xaml:
  84. <UserControl x:Class="BankApp.Components.NavButton"
  85.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  86.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  87.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  88.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  89.              xmlns:local="clr-namespace:BankApp.Components"
  90.              mc:Ignorable="d"
  91.              x:Name ="root"
  92.              >
  93.  
  94.     <local:BaseButton
  95.         Content="{Binding label, ElementName=root}"
  96.  
  97.     />
  98.  
  99. </UserControl>
  100.  
  101. class:
  102. using BankApp.Core;
  103. using BankApp.DependancyClasses;
  104. using System;
  105. using System.Collections.Generic;
  106. using System.Linq;
  107. using System.Text;
  108. using System.Threading.Tasks;
  109. using System.Windows;
  110. using System.Windows.Controls;
  111. using System.Windows.Data;
  112. using System.Windows.Documents;
  113. using System.Windows.Input;
  114. using System.Windows.Media;
  115. using System.Windows.Media.Imaging;
  116. using System.Windows.Navigation;
  117. using System.Windows.Shapes;
  118.  
  119. namespace BankApp.Components
  120. {
  121.  
  122.     public partial class NavButton : UserControl
  123.     {
  124.         // ------------------- Properties -------------------
  125.         public string label
  126.         {
  127.             get { return (string)GetValue(mDepProp_BtnLabel); }
  128.             set { SetValue(mDepProp_BtnLabel, value); }
  129.         }
  130.         public NavType type
  131.         {
  132.             get { return (NavType)GetValue(mDepProp_BtnType); }
  133.             set { SetValue(mDepProp_BtnType, value); }
  134.         }
  135.         //public Style BtnStyle
  136.         //{
  137.         //    get { return (Style)GetValue(mDepProp_BtnStyle); }
  138.         //    set { SetValue(mDepProp_BtnStyle, value); }
  139.         //}
  140.  
  141.         public event EventHandler<NavClickedEventArgs> onClick;
  142.  
  143.  
  144.         // ------------------- Dependency Properties -------------------
  145.         public static readonly DependencyProperty mDepProp_BtnLabel =
  146.             DependencyProperty.Register(
  147.                 "label",
  148.                 typeof(string),
  149.                 typeof(NavButton),
  150.                 new PropertyMetadata("Default Label"));
  151.  
  152.  
  153.         public static readonly DependencyProperty mDepProp_BtnType =
  154.             DependencyProperty.Register(
  155.                 "type",
  156.                 typeof(NavType),
  157.                 typeof(NavButton),
  158.                 new PropertyMetadata(NavType.HOME));
  159.  
  160.  
  161.  
  162.         // Using a DependencyProperty as the backing store for BtnStyle.  This enables animation, styling, binding, etc...
  163. /*        public static readonly DependencyProperty mDepProp_BtnStyle =
  164.             DependencyProperty.Register(
  165.                 "BtnStyle",
  166.                 typeof(Style),
  167.                 typeof(NavButton),
  168.                 new PropertyMetadata(null));*/
  169.  
  170.  
  171.  
  172.         public NavButton()
  173.         {
  174.             InitializeComponent();
  175.         }
  176.  
  177.  
  178.         // ------------------- Event Handlers -------------------
  179.         private void OnBtnClick(object sender, RoutedEventArgs e)
  180.         {
  181.             var args = new NavClickedEventArgs(label, type);
  182.             MessageBox.Show("Button Clicked");
  183.             onClick?.Invoke(this, args);
  184.         }
  185.     }
  186. }
  187.  
  188. // Base Button ===============
  189. xaml:
  190.  
  191. <UserControl x:Class="BankApp.Components.BaseButton"
  192.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  193.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  194.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  195.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  196.              xmlns:local="clr-namespace:BankApp.Components"
  197.              mc:Ignorable="d"
  198.              d:DesignHeight="450" d:DesignWidth="800">
  199.     <Button
  200.         Click="OnBtnClick"
  201.         Content="{Binding label, ElementName=root}"
  202.         Style="{Binding btnStyle, ElementName=root}"
  203.         />
  204. </UserControl>
  205.  
  206.  
  207. class:
  208. using System;
  209. using System.Collections.Generic;
  210. using System.Linq;
  211. using System.Text;
  212. using System.Threading.Tasks;
  213. using System.Windows;
  214. using System.Windows.Controls;
  215. using System.Windows.Data;
  216. using System.Windows.Documents;
  217. using System.Windows.Input;
  218. using System.Windows.Media;
  219. using System.Windows.Media.Imaging;
  220. using System.Windows.Navigation;
  221. using System.Windows.Shapes;
  222.  
  223. namespace BankApp.Components
  224. {
  225.     /// <summary>
  226.     /// Interaction logic for BaseButton.xaml
  227.     /// </summary>
  228.     public partial class BaseButton : UserControl
  229.     {
  230.         public string label
  231.         {
  232.             get { return (string)GetValue(mDepProp_BtnLabel); }
  233.             set { SetValue(mDepProp_BtnLabel, value); }
  234.         }
  235.  
  236.         public Style btnStyle
  237.         {
  238.             get { return (Style)GetValue(mDepProp_BtnStyle); }
  239.             set { SetValue(mDepProp_BtnStyle, value); }
  240.         }
  241.  
  242.         public static readonly DependencyProperty mDepProp_BtnLabel =
  243.             DependencyProperty.Register(
  244.                 "label",
  245.                 typeof(string),
  246.                 typeof(BaseButton),
  247.                 new PropertyMetadata("Default Button"));
  248.  
  249.         public static readonly DependencyProperty mDepProp_BtnStyle =
  250.             DependencyProperty.Register(
  251.                 "btnStyle",
  252.                 typeof(Style),
  253.                 typeof(BaseButton),
  254.                 new PropertyMetadata(null, OnStyleChanged));
  255.  
  256.         private static void OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  257.         {
  258.             if (d is BaseButton btn && e.NewValue is Style style)
  259.             {
  260.                 btn.btnStyle = style;
  261.             }
  262.         }
  263.  
  264.         public BaseButton()
  265.         {
  266.             InitializeComponent();
  267.         }
  268.  
  269.         private void OnBtnClick(object sender, RoutedEventArgs e)
  270.         {
  271.         }
  272.     }
  273. }
  274.  
  275. the Style:
  276.  
  277. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  278.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  279.  
  280.  
  281.     <Style x:Key="NavButtonStyle" TargetType="Button">
  282.         <Setter Property="Content" Value="Nav"/>
  283.         <Setter Property="Background" Value="Gray"/>
  284.         <Setter Property="Foreground" Value="White"/>
  285.         <Setter Property="Margin" Value="2"/>
  286.         <Setter Property="Padding" Value="2"/>
  287.         <Setter Property="HorizontalAlignment" Value="Stretch"/>
  288.         <Setter Property="VerticalAlignment" Value="Stretch"/>
  289.     </Style>
  290.    
  291. </ResourceDictionary>
Advertisement
Add Comment
Please, Sign In to add comment