Last active
February 15, 2021 22:21
-
-
Save jasper-zanjani/598061d140f305ec50b4605a7a8c6490 to your computer and use it in GitHub Desktop.
DC Superhero ListView and TextBoxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<UserControl | |
x:Class="Superheroes.HeroDetailControl" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="using:Superheroes" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
mc:Ignorable="d" | |
d:DesignHeight="300" | |
d:DesignWidth="400" | |
> | |
<StackPanel> | |
<TextBox | |
x:Name="FirstNameTextbox" | |
Header="First name" | |
Margin="10" | |
Text="{x:Bind Hero.FirstName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" | |
GotFocus="Textbox_GotFocus" | |
/> | |
<TextBox | |
x:Name="LastNameTextbox" | |
Header="Last name" | |
Margin="10" | |
Text="{x:Bind Hero.LastName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" | |
GotFocus="Textbox_GotFocus" | |
/> | |
<TextBox | |
x:Name="SuperheroTextbox" | |
Header="Alias" | |
Margin="10" | |
Text="{x:Bind Hero.Superhero,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" | |
GotFocus="Textbox_GotFocus" | |
/> | |
</StackPanel> | |
</UserControl> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Controls; | |
namespace Superheroes | |
{ | |
public sealed partial class HeroDetailControl : UserControl | |
{ | |
public Hero Hero | |
{ | |
get { return (Hero)GetValue(HeroProperty); } | |
set { SetValue(HeroProperty, value); } | |
} | |
public static readonly DependencyProperty HeroProperty = | |
DependencyProperty.Register("Hero", typeof(Hero), typeof(HeroDetailControl), new PropertyMetadata(null, HeroChangedCallback)); | |
private static void HeroChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
} | |
public HeroDetailControl() | |
{ | |
this.InitializeComponent(); | |
} | |
private void Textbox_GotFocus(object sender, RoutedEventArgs e) | |
{ | |
TextBox txtbox = sender as TextBox; | |
txtbox.SelectAll(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Page | |
x:Class="Superheroes.MainPage" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="using:Superheroes" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
mc:Ignorable="d" | |
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> | |
<StackPanel> | |
<CommandBar> | |
<AppBarButton x:Name="AddHero" Click="AddHero_Click" Label="Add hero"> | |
<SymbolIcon Symbol="Add"/> | |
</AppBarButton> | |
<AppBarButton x:Name="DelHero" Click="DelHero_Click" Label="Remove hero" IsEnabled="{x:Bind ViewModel.IsHeroSelected,Mode=OneWay}"> | |
<SymbolIcon Symbol="Delete"/> | |
</AppBarButton> | |
</CommandBar> | |
<local:HeroDetailControl Hero="{Binding ElementName=heroesListView,Path=SelectedItem,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> | |
<ListView | |
x:Name="heroesListView" | |
ItemsSource="{x:Bind ViewModel.Heroes,Mode=OneWay}" | |
SelectedItem="{x:Bind ViewModel.SelectedHero,Mode=TwoWay}" | |
Margin="0" | |
> | |
<ListView.ItemTemplate> | |
<DataTemplate x:DataType="local:Hero"> | |
<StackPanel Orientation="Horizontal"> | |
<TextBlock Text="{x:Bind Superhero,Mode=OneWay}" FontWeight="Bold"/> | |
<TextBlock Text="{x:Bind FirstName,Mode=OneWay}" Margin="5 0 0 0"/> | |
<TextBlock Text="{x:Bind LastName,Mode=OneWay}" Margin="5 0 0 0"/> | |
</StackPanel> | |
</DataTemplate> | |
</ListView.ItemTemplate> | |
</ListView> | |
</StackPanel> | |
</Page> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.ObjectModel; | |
using System.ComponentModel; | |
using System.Runtime.CompilerServices; | |
using Windows.UI.Xaml.Controls; | |
namespace Superheroes | |
{ | |
public class Observable : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected void OnPropertyChanged([CallerMemberName] string propertyName = null) | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} | |
public class Hero : Observable | |
{ | |
private string superhero; | |
private string lastName; | |
private string firstName; | |
public string FirstName | |
{ | |
get => firstName; set | |
{ | |
firstName = value; | |
OnPropertyChanged(); | |
} | |
} | |
public string LastName | |
{ | |
get => lastName; set | |
{ | |
lastName = value; | |
OnPropertyChanged(); | |
} | |
} | |
public string Superhero | |
{ | |
get => superhero; set | |
{ | |
superhero = value; | |
OnPropertyChanged(); | |
} | |
} | |
public Hero(string fn, string ln, string s) | |
{ | |
FirstName = fn; | |
LastName = ln; | |
Superhero = s; | |
} | |
public Hero() : this("John", "Doe", "Captain Underpants") { } | |
} | |
public class ViewModel : Observable | |
{ | |
public bool IsHeroSelected => SelectedHero != null; | |
public ObservableCollection<Hero> Heroes { get; } | |
private Hero _selectedHero; | |
public Hero SelectedHero | |
{ | |
get { return _selectedHero; } | |
set | |
{ | |
_selectedHero = value; | |
OnPropertyChanged(); | |
OnPropertyChanged(nameof(IsHeroSelected)); | |
} | |
} | |
public ViewModel() | |
{ | |
Heroes = new ObservableCollection<Hero>(); | |
Heroes.Add(new Hero("Clark","Kent","Superman")); | |
Heroes.Add(new Hero("Bruce", "Wayne", "Batman")); | |
Heroes.Add(new Hero("Hal", "Jordan", "Green Lantern")); | |
Heroes.Add(new Hero("Diana", "Prince", "Wonder Woman")); | |
} | |
} | |
public sealed partial class MainPage : Page | |
{ | |
public ViewModel ViewModel { get; } | |
public MainPage() | |
{ | |
this.InitializeComponent(); | |
this.ViewModel = new ViewModel(); | |
DataContext = ViewModel; | |
} | |
private void AddHero_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) | |
{ | |
var hero = new Hero(); | |
ViewModel.Heroes.Add(hero); | |
ViewModel.SelectedHero = hero; | |
} | |
private void DelHero_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) | |
{ | |
ViewModel.Heroes.Remove(ViewModel.SelectedHero); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment