diff --git a/WinUIGallery/Assets/ControlImages/CodeTagIcon.png b/WinUIGallery/Assets/ControlImages/CodeTagIcon.png new file mode 100644 index 000000000..d162f5876 Binary files /dev/null and b/WinUIGallery/Assets/ControlImages/CodeTagIcon.png differ diff --git a/WinUIGallery/Assets/SampleMedia/Dark_Image.png b/WinUIGallery/Assets/SampleMedia/Dark_Image.png new file mode 100644 index 000000000..14ff2e2ed Binary files /dev/null and b/WinUIGallery/Assets/SampleMedia/Dark_Image.png differ diff --git a/WinUIGallery/Assets/SampleMedia/Light_Image.png b/WinUIGallery/Assets/SampleMedia/Light_Image.png new file mode 100644 index 000000000..7d2136a5a Binary files /dev/null and b/WinUIGallery/Assets/SampleMedia/Light_Image.png differ diff --git a/WinUIGallery/ControlPages/XamlFundamentals/BindingPage.xaml b/WinUIGallery/ControlPages/XamlFundamentals/BindingPage.xaml new file mode 100644 index 000000000..6829fd54f --- /dev/null +++ b/WinUIGallery/ControlPages/XamlFundamentals/BindingPage.xaml @@ -0,0 +1,199 @@ + + + + + + + Key concepts: + 1. Target: The property of a control to which data is bound (e.g., Text, Background, Visibility). + 2. Source: The data being bound, such as a property in a class, another control, or a static resource. + 3. Binding Modes: + - OneWay updates the target when the source changes. + - TwoWay updates both the target and the source. + - OneTime sets the target once and does not update afterward. + + + + {x:Bind} and {Binding} feature comparison + + + + + + + + Microsoft Learn + . + + + + + + + + + + + + + + +<TextBox x:Name="SourceTextBox" Width="200" PlaceholderText="Enter text here" /> +<TextBox Width="200" Text="{x:Bind SourceTextBox.Text, Mode=$(BindingMode)}" PlaceholderText="Mirrors above text" /> + + + + + OneWay + TwoWay + + + + + + + + + + + + + + + +<TextBlock + Text="{x:Bind GreetingMessage}" + FontSize="24" + HorizontalAlignment="Center" + VerticalAlignment="Center" /> + + + + +// Code-behind +public string GreetingMessage { get; set; } = "Hello, WinUI 3!"; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +<TextBlock Text="Title:" FontWeight="SemiBold" /> +<TextBlock Text="{Binding Title}" FontSize="16" /> + +<TextBlock Text="Description:" FontWeight="SemiBold" /> +<TextBlock Text="{Binding Description}" FontSize="16" /> + + + + + + + + + + + + + + + + + + +<!--FallbackValue and TargetNullValue properties help handle scenarios where the binding source is missing or null--> +<TextBlock Text="{Binding NonExistentProperty, FallbackValue='Default Text'}" /> +<TextBlock Text="{Binding NullString, TargetNullValue='Anonymous User'}" /> + + + + +public YourPage() +{ + this.InitializeComponent(); + + ViewModel = new ExampleViewModel + { + NullString = null + }; + + DataContext = ViewModel; +} + + + + + diff --git a/WinUIGallery/ControlPages/XamlFundamentals/BindingPage.xaml.cs b/WinUIGallery/ControlPages/XamlFundamentals/BindingPage.xaml.cs new file mode 100644 index 000000000..1aa070ac0 --- /dev/null +++ b/WinUIGallery/ControlPages/XamlFundamentals/BindingPage.xaml.cs @@ -0,0 +1,161 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Data; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Navigation; +using Microsoft.UI.Xaml.Documents; +using System.ComponentModel; +using Microsoft.UI.Text; + +namespace WinUIGallery.ControlPages +{ + public sealed partial class BindingPage : Page + { + public string GreetingMessage { get; set; } = "Hello, WinUI 3!"; + + public ExampleViewModel ViewModel { get; set; } + + public BindingPage() + { + this.InitializeComponent(); + + ViewModel = new ExampleViewModel + { + Title = "Welcome to WinUI 3", + Description = "This is an example of binding to a view model.", + NullString = null + }; + DataContext = ViewModel; + } + + private void BindingModeGroup_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (sender is RadioButtons radioButtons) + { + switch (radioButtons.SelectedIndex) + { + case 0: + UpdateBindingAndDescription(BindingMode.OneWay); + break; + case 1: + UpdateBindingAndDescription(BindingMode.TwoWay); + break; + } + } + } + + private void UpdateBindingAndDescription(BindingMode bindingMode) + { + var binding = new Binding + { + Source = SourceTextBox, + Path = new PropertyPath("Text"), + Mode = bindingMode, + }; + TargetTextBox.SetBinding(TextBox.TextProperty, binding); + + BindingModeDescription.Blocks.Clear(); + var paragraph = new Paragraph(); + if (bindingMode == BindingMode.OneWay) + { + paragraph.Inlines.Add(new Run + { + Text = "In ", + }); + paragraph.Inlines.Add(new Run { Text = "OneWay", FontWeight = FontWeights.SemiBold }); + paragraph.Inlines.Add(new Run + { + Text = " binding mode, changes in the source (SourceTextBox) are reflected in the target, but not vice versa." + }); + } + else if (bindingMode == BindingMode.TwoWay) + { + paragraph.Inlines.Add(new Run + { + Text = "In ", + }); + paragraph.Inlines.Add(new Run { Text = "TwoWay", FontWeight = FontWeights.SemiBold }); + paragraph.Inlines.Add(new Run + { + Text = " binding mode, changes in either box update the other." + }); + } + + BindingModeDescription.Blocks.Add(paragraph); + } + + public string FormatDate(DateTimeOffset? date) + { + if (date.HasValue) + { + return "Selected date is: " + date.Value.ToString("dddd, MMMM d, yyyy"); + } + else + { + return "No date selected"; + } + } + } + + public partial class ExampleViewModel : INotifyPropertyChanged + { + private string _title; + private string _description; + private string _nullString; + + public string Title + { + get => _title; + set + { + if (_title != value) + { + _title = value; + OnPropertyChanged(nameof(Title)); + } + } + } + + public string Description + { + get => _description; + set + { + if (_description != value) + { + _description = value; + OnPropertyChanged(nameof(Description)); + } + } + } + + public string NullString + { + get => _nullString; + set + { + if (_nullString != value) + { + _nullString = value; + OnPropertyChanged(nameof(_nullString)); + } + } + } + + public event PropertyChangedEventHandler PropertyChanged; + + protected void OnPropertyChanged(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } +} diff --git a/WinUIGallery/ControlPages/XamlFundamentals/TemplatesPage.xaml b/WinUIGallery/ControlPages/XamlFundamentals/TemplatesPage.xaml new file mode 100644 index 000000000..0e70b2d95 --- /dev/null +++ b/WinUIGallery/ControlPages/XamlFundamentals/TemplatesPage.xaml @@ -0,0 +1,158 @@ + + + + + + + + + + + + + + + + + Placement of Templates: + Templates can be defined at the app, page, or control level, similar to styles and resources. The placement is determined by the intended scope and reuse of the template. + + + + There are 3 types of templates: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Option 1 + Option 2 + Option 3 + + + + + + + + + + + + + + + + + Item 01 + Item 02 + Item 03 + Item 04 + Item 05 + Item 06 + Item 07 + Item 08 + Item 09 + Item 10 + Item 11 + Item 12 + Item 13 + Item 14 + Item 15 + Item 16 + Item 17 + Item 18 + Item 19 + Item 20 + + + + + + + + + + + + diff --git a/WinUIGallery/ControlPages/XamlFundamentals/TemplatesPage.xaml.cs b/WinUIGallery/ControlPages/XamlFundamentals/TemplatesPage.xaml.cs new file mode 100644 index 000000000..c4996c344 --- /dev/null +++ b/WinUIGallery/ControlPages/XamlFundamentals/TemplatesPage.xaml.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Data; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Navigation; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Windows.Storage; + +namespace WinUIGallery.ControlPages +{ + public sealed partial class TemplatesPage : Page + { + public TemplatesPage() + { + this.InitializeComponent(); + } + + private void LayoutSelector_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (e.AddedItems[0] is RadioButton selectedRadioButton) + { + // Check the tag of the selected RadioButton + if (selectedRadioButton.Tag.ToString() == "WrapGrid") + { + MyListView.ItemsPanel = (ItemsPanelTemplate)Resources["WrapGridTemplate"]; + Example3.Xaml = ReadSampleCodeFileContent("TemplatesSample3_WrapGrid_xaml"); + } + else if (selectedRadioButton.Tag.ToString() == "StackPanel") + { + MyListView.ItemsPanel = (ItemsPanelTemplate)Resources["StackPanelTemplate"]; + Example3.Xaml = ReadSampleCodeFileContent("TemplatesSample3_StackPanel_xaml"); + } + } + } + + private static string ReadSampleCodeFileContent(string sampleCodeFileName) + { + StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation; + return File.ReadAllText($"{folder.Path}\\ControlPagesSampleCode\\Templates\\{sampleCodeFileName}.txt"); + } + } +} diff --git a/WinUIGallery/ControlPages/XamlFundamentals/XamlResourcesPage.xaml b/WinUIGallery/ControlPages/XamlFundamentals/XamlResourcesPage.xaml new file mode 100644 index 000000000..c1205eb1b --- /dev/null +++ b/WinUIGallery/ControlPages/XamlFundamentals/XamlResourcesPage.xaml @@ -0,0 +1,229 @@ + + + + + #FF5733 + + + + 24 + #4CAF50 + + WinUI 3 Gallery + + Welcome to this page + + + #2196F3 + + + + + + + + + + + + + + + + + + + + • App-level: Resources are defined globally, accessible throughout the application. + • Page-level: Resources are defined specific to a particular page. + • Control-level: Resources are defined local to a specific control, such as a Button or Grid. + + + + Tips: + • Naming: descriptive keys should always be used for resources to make them easier to identify. + • Scope: Resources should be defined at the narrowest scope possible to improve maintainability. + + + + + + + + + + + + + + + + + + + + + + + + + 14 + Using control-level resources + + + + + + + + + + + Definition of a Resource Dictionary: + + + + + + • Inline (page-level): Defined in a specific page to limit the scope to that page. + + + + + + + + + Reasons for using resource dictionaries: + • Reusability: define once and use many times. + • Organization: keeps resources modular and separated. + • Maintainability: makes it easier to update values across the app. + + + + + + + + + + + + + + + + + + + + + + + + + + WinUI 3 includes built-in theme resources for commonly used colors. See all brushes on the Color page + . + + + + + + + + + + + + + + • A fallback value should always be provided to ensure compatibility with undefined themes. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Light theme + ms-appx:///Assets/SampleMedia/Light_Image.png + + + + + Dark theme + ms-appx:///Assets/SampleMedia/Dark_Image.png + + + + + + + + + + + + + + diff --git a/WinUIGallery/ControlPages/XamlFundamentals/XamlResourcesPage.xaml.cs b/WinUIGallery/ControlPages/XamlFundamentals/XamlResourcesPage.xaml.cs new file mode 100644 index 000000000..0815be71d --- /dev/null +++ b/WinUIGallery/ControlPages/XamlFundamentals/XamlResourcesPage.xaml.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Data; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Navigation; +using Windows.Foundation; +using Windows.Foundation.Collections; + +// To learn more about WinUI, the WinUI project structure, +// and more about our project templates, see: http://aka.ms/winui-project-info. + +namespace WinUIGallery.ControlPages +{ + /// + /// An empty page that can be used on its own or navigated to within a Frame. + /// + public sealed partial class XamlResourcesPage : Page + { + public XamlResourcesPage() + { + this.InitializeComponent(); + } + + private void Hyperlink_Click(Microsoft.UI.Xaml.Documents.Hyperlink sender, Microsoft.UI.Xaml.Documents.HyperlinkClickEventArgs args) + { + NavigationRootPage.GetForElement(this).Navigate(typeof(ItemPage), "Color"); + } + } +} diff --git a/WinUIGallery/ControlPages/XamlFundamentals/XamlStylesPage.xaml b/WinUIGallery/ControlPages/XamlFundamentals/XamlStylesPage.xaml new file mode 100644 index 000000000..01cb55791 --- /dev/null +++ b/WinUIGallery/ControlPages/XamlFundamentals/XamlStylesPage.xaml @@ -0,0 +1,112 @@ + + + + + + + The definition of styles is similiar to other resources: app-level, page-level, control-level. + + + + + + + + + + + + + + + + • Styles improve maintainability, consistency, and reduce repetition in XAML code. + + + + + + + + + +