How to create a completly new project
Step-by-step
- Open Visual Studio (Obvious)
- Create a new project (solution)
- In the menu where you can see all different types of project, choose
WpfCustomControlLibrary
WARNING
The Generic.xaml should be created automatically. Otherwise you have choosen a false project type!
Examples
DANGER
The examples provided are syntax wise correct, but the login screen logic does not meet the requirements which were given in the assignment.
The Custom Control -> CustomControl1.cs
csharp
using System.Text;
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 LoginRegisterLibrary
{
public class LoginControl : Control
{
static LoginControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LoginControl),
new FrameworkPropertyMetadata(typeof(LoginControl)));
}
// Public Properties
public string UsernameOrEmail
{
get { return (string)GetValue(UsernameOrEmailProperty); }
set { SetValue(UsernameOrEmailProperty, value); }
}
public static readonly DependencyProperty UsernameOrEmailProperty =
DependencyProperty.Register(nameof(UsernameOrEmail), typeof(string), typeof(LoginControl));
public string Password
{
get { return (string)GetValue(PasswordProperty); }
set { SetValue(PasswordProperty, value); }
}
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.Register(nameof(Password), typeof(string), typeof(LoginControl));
public bool UseUsername
{
get { return (bool)GetValue(UseUsernameProperty); }
set { SetValue(UseUsernameProperty, value); }
}
public static readonly DependencyProperty UseUsernameProperty =
DependencyProperty.Register(nameof(UseUsername), typeof(bool), typeof(LoginControl), new PropertyMetadata(false));
// Events
public event EventHandler LoginClicked;
public event EventHandler SwitchToRegistrationClicked;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var btnLogin = GetTemplateChild("PART_LoginButton") as Button;
var btnSwitch = GetTemplateChild("PART_SwitchButton") as Button;
if (btnLogin != null) btnLogin.Click += (s, e) => OnLoginClicked();
if (btnSwitch != null) btnSwitch.Click += (s, e) => SwitchToRegistrationClicked?.Invoke(this, EventArgs.Empty);
}
private void OnLoginClicked()
{
if (string.IsNullOrWhiteSpace(UsernameOrEmail) || string.IsNullOrWhiteSpace(Password))
{
MessageBox.Show("Bitte Benutzername/E-Mail und Passwort eingeben.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
LoginClicked?.Invoke(this, EventArgs.Empty);
}
}
}The Generic -> Generic.xaml
xml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LoginRegisterLibrary">
<Style TargetType="{x:Type local:LoginControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:LoginControl}">
<StackPanel Margin="10" Width="300">
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=UseUsername}" Margin="0,0,0,5"/>
<TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=UsernameOrEmail, Mode=TwoWay}" Margin="0,0,0,5"/>
<TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Password, Mode=TwoWay}" Margin="0,0,0,5"/>
<Button x:Name="PART_LoginButton" Content="Login" Margin="0,5,0,0"/>
<Button x:Name="PART_SwitchButton" Content="Zum Registrieren" Margin="0,5,0,0"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>Use in the main (working) project -> mainWindow.xaml
INFO
It is important that you add this line in the first opening 'Window' tag, otherwise the following steps won't work. You need to replace 'LoginRegisterLibrary' with the Namespacename of your project.
xml
xmlns:LoginRegisterLibrary="clr-namespace:LoginRegisterLibrary;assembly=LoginRegisterLibrary"xml
<!--Now you can access your Custom Resource by using this: -->
<!--Make sure to replace `LoginRegisterLibrary` with your namespace and 'LoginControl' with the classname! -->
<LoginRegisterLibrary:LoginControl x:Name="loginControl"/>