Skip to content

Instantly share code, notes, and snippets.

@rmcdongit
Created December 11, 2020 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmcdongit/cda08aff38c01d962f28d2782c9a2457 to your computer and use it in GitHub Desktop.
Save rmcdongit/cda08aff38c01d962f28d2782c9a2457 to your computer and use it in GitHub Desktop.
WPF Custom Classes
<Page x:Class="WpfApp2.Factory"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Factory">
<Grid>
<Label Content="Enter a product name:" HorizontalAlignment="Left" Margin="105,98,0,0" VerticalAlignment="Top" Height="37" Width="214" FontSize="20"/>
<TextBox x:Name="tbxProductName" HorizontalAlignment="Left" Height="23" Margin="117,140,0,0" TextWrapping="Wrap" Text="Product Name" VerticalAlignment="Top" Width="120" TextChanged="tbxProductName_TextChanged"/>
<Button x:Name="btnBuild" Content="Build" HorizontalAlignment="Left" Margin="117,296,0,0" VerticalAlignment="Top" Width="75" Click="btnBuild_Click"/>
<Button x:Name="btnQuit" Content="Quit" HorizontalAlignment="Left" Margin="362,296,0,0" VerticalAlignment="Top" Width="75" Click="btnQuit_Click"/>
<StackPanel HorizontalAlignment="Left" Height="40" Margin="117,211,0,0" VerticalAlignment="Top" Width="100">
<RadioButton Content="Red" Checked="Color"/>
<RadioButton Content="Blue" Checked="Color"/>
</StackPanel>
<Label Content="Pick a Color:" HorizontalAlignment="Left" Margin="117,178,0,0" VerticalAlignment="Top" Width="177"/>
</Grid>
</Page>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp2
{
/// <summary>
/// Interaction logic for Factory.xaml
/// </summary>
public partial class Factory : Page
{
Widget Product = new Widget();
public Factory()
{
InitializeComponent();
}
private void tbxProductName_TextChanged(object sender, TextChangedEventArgs e)
{
Product.Name = tbxProductName.Text;
}
private void Color(object sender, RoutedEventArgs e)
{
var Button = sender as RadioButton;
Product.Color = Button.Content.ToString();
}
private void btnBuild_Click(object sender, RoutedEventArgs e)
{
// Launch our Store
NavigationService.Navigate(new Store(Product));
}
private void btnQuit_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
}
}
}
<Window x:Class="WpfApp2.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Background="Fuchsia">
<Frame x:Name="Navigation" NavigationUIVisibility="Hidden"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Navigation.Navigate(new Factory());
}
}
}
<Page x:Class="WpfApp2.Store"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Store">
<Grid>
<Label Content="Product Name:" HorizontalAlignment="Left" Margin="148,122,0,0" VerticalAlignment="Top" Width="160" Height="36" FontSize="22"/>
<TextBlock x:Name="tblkProductName" HorizontalAlignment="Left" Margin="336,122,0,0" TextWrapping="Wrap" Text="Product Name" VerticalAlignment="Top" Height="36" Width="218" FontSize="20"/>
<Ellipse x:Name="ellProductColor" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="349,208,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
<Label Content="Product Color:" HorizontalAlignment="Left" Margin="148,227,0,0" VerticalAlignment="Top" Width="160" Height="36" FontSize="22"/>
<Button x:Name="btnBack" Content="Go Back" HorizontalAlignment="Left" Margin="375,361,0,0" VerticalAlignment="Top" Width="75" Click="btnBack_Click"/>
</Grid>
</Page>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp2
{
/// <summary>
/// Interaction logic for Store.xaml
/// </summary>
public partial class Store : Page
{
BrushConverter BC = new BrushConverter();
public Store(Widget P)
{
InitializeComponent();
LoadProduct(P);
}
private void LoadProduct(Widget P)
{
SolidColorBrush clr = BC.ConvertFromString(P.Color) as SolidColorBrush;
tblkProductName.Text = P.Name;
ellProductColor.Fill = clr;
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
NavigationService.GoBack();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp2
{
public class Widget
{
public string Name;
public string Color = "White";
public Widget()
{
//TODO: Constructor
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment