diff --git a/.markdownlint-cli2.jsonc b/.markdownlint-cli2.jsonc index 5d410e977f..7f05a5ac18 100644 --- a/.markdownlint-cli2.jsonc +++ b/.markdownlint-cli2.jsonc @@ -14,6 +14,7 @@ "MD026": false, // Cannot be enabled due to sequential Note blocks "MD028": false, + "MD030": false, "MD033": { "allowed_elements": [ "a", diff --git a/dotnet-desktop-guide/framework/wpf/advanced/how-to-detect-when-the-enter-key-pressed.md b/dotnet-desktop-guide/framework/wpf/advanced/how-to-detect-when-the-enter-key-pressed.md index 93ad776028..e36d72e974 100644 --- a/dotnet-desktop-guide/framework/wpf/advanced/how-to-detect-when-the-enter-key-pressed.md +++ b/dotnet-desktop-guide/framework/wpf/advanced/how-to-detect-when-the-enter-key-pressed.md @@ -1,7 +1,7 @@ --- title: "How to: Detect When the Enter Key Pressed" description: Detect when the Enter key is selected on the keyboard in Windows Presentation Foundation. This example consists of XAML and a code-behind file. -ms.date: "03/30/2017" +ms.date: 07/16/2024 dev_langs: - "csharp" - "vb" @@ -9,26 +9,29 @@ helpviewer_keywords: - "Enter key [WPF], detecting" - "keys [WPF], Enter" ms.assetid: a66f39d2-ef4a-43a5-b454-a4ea0fe88655 +# TODO: +# When upgrading this article to .NET, add more examples such as doing a global handler, attached behavior, input command, etc> +# --- # How to: Detect When the Enter Key Pressed -This example shows how to detect when the key is pressed on the keyboard. - - This example consists of a Extensible Application Markup Language (XAML) file and a code-behind file. - -## Example - - When the user presses the key in the , the input in the text box appears in another area of the user interface (UI). - - The following XAML creates the user interface, which consists of a , a , and a . - - [!code-xaml[keydown#KeyDownUI](~/samples/snippets/csharp/VS_Snippets_Wpf/KeyDown/CSharp/Window1.xaml#keydownui)] - - The following code behind creates the event handler. If the key that is pressed is the key, a message is displayed in the . - - [!code-csharp[keydown#KeyDownSample](~/samples/snippets/csharp/VS_Snippets_Wpf/KeyDown/CSharp/Window1.xaml.cs#keydownsample)] - [!code-vb[keydown#KeyDownSample](~/samples/snippets/visualbasic/VS_Snippets_Wpf/KeyDown/VisualBasic/Window1.xaml.vb#keydownsample)] - +This example shows how to detect when the key is pressed on the keyboard. + +This example consists of a Extensible Application Markup Language (XAML) file and a code-behind file. + +## Example + +When the user presses the key in the , the input in the text box appears in another area of the user interface (UI). + +The following XAML creates the user interface, which consists of a , a , and a . + +:::code language="xaml" source="./snippets/how-to-detect-when-the-enter-key-pressed/csharp/MainWindow.xaml" id="example"::: + +The following code behind creates the event handler. If the key that is pressed is the key, a message is displayed in the . + +:::code language="csharp" source="./snippets/how-to-detect-when-the-enter-key-pressed/csharp/MainWindow.xaml.cs" id="handler"::: +:::code language="vb" source="./snippets/how-to-detect-when-the-enter-key-pressed/vb/MainWindow.xaml.vb" id="handler"::: + ## See also - [Input Overview](input-overview.md) diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/App.xaml b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/App.xaml new file mode 100644 index 0000000000..6bfa8b564e --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/App.xaml @@ -0,0 +1,8 @@ + + + + + diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/App.xaml.cs b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/App.xaml.cs new file mode 100644 index 0000000000..94d1057d50 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/App.xaml.cs @@ -0,0 +1,14 @@ +using System.Configuration; +using System.Data; +using System.Windows; + +namespace SDKSamples +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } + +} diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/AssemblyInfo.cs b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/AssemblyInfo.cs new file mode 100644 index 0000000000..cc29e7f741 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly:ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/MainWindow.xaml b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/MainWindow.xaml new file mode 100644 index 0000000000..c346c14ae2 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/MainWindow.xaml @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/MainWindow.xaml.cs b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/MainWindow.xaml.cs new file mode 100644 index 0000000000..0052a61df0 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/MainWindow.xaml.cs @@ -0,0 +1,30 @@ +// +using System; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Input; + +namespace SDKSamples +{ + public partial class MainWindow : Window + { + public MainWindow() => + InitializeComponent(); + + private void Window_Loaded(object sender, RoutedEventArgs e) + { + } + + // + private void textBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) + { + if (e.Key == Key.Enter) + { + textBlock1.Text = $"You Entered: {textBox1.Text}"; + } + } + // + } +} +// diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/ProjectCS.csproj b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/ProjectCS.csproj new file mode 100644 index 0000000000..0336b4ecc2 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/ProjectCS.csproj @@ -0,0 +1,11 @@ + + + + WinExe + net481 + true + SDKSamples + app.manifest + + + diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/app.manifest b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/app.manifest new file mode 100644 index 0000000000..c522bb43f3 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/csharp/app.manifest @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/App.config b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/App.config new file mode 100644 index 0000000000..aee9adf485 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/Application.xaml b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/Application.xaml new file mode 100644 index 0000000000..e20c9143a9 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/Application.xaml @@ -0,0 +1,8 @@ + + + + + diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/Application.xaml.vb b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/Application.xaml.vb new file mode 100644 index 0000000000..084cbe917e --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/Application.xaml.vb @@ -0,0 +1,6 @@ +Class Application + + ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException + ' can be handled in this file. + +End Class diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/AssemblyInfo.vb b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/AssemblyInfo.vb new file mode 100644 index 0000000000..025ee7271e --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/AssemblyInfo.vb @@ -0,0 +1,11 @@ +Imports System.Windows + +'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found. +'1st parameter: where theme specific resource dictionaries are located +'(used if a resource is not found in the page, +' or application resource dictionaries) + +'2nd parameter: where the generic resource dictionary is located +'(used if a resource is not found in the page, +'app, and any theme specific resource dictionaries) + diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/MainWindow.xaml b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/MainWindow.xaml new file mode 100644 index 0000000000..07dd8a1dfb --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/MainWindow.xaml @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/MainWindow.xaml.vb b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/MainWindow.xaml.vb new file mode 100644 index 0000000000..5358a5e134 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/MainWindow.xaml.vb @@ -0,0 +1,18 @@ +' +Imports System.Threading + +Public Class MainWindow + Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) + End Sub + + ' + Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs) + + If e.Key = Key.Return Then + textBlock1.Text = "You Entered: " + textBox1.Text + End If + + End Sub + ' +End Class +' diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/AssemblyInfo.vb b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/AssemblyInfo.vb new file mode 100644 index 0000000000..0c518abf27 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/AssemblyInfo.vb @@ -0,0 +1,59 @@ +Imports System +Imports System.Globalization +Imports System.Reflection +Imports System.Resources +Imports System.Runtime.InteropServices +Imports System.Windows + +' General Information about an assembly is controlled through the following +' set of attributes. Change these attribute values to modify the information +' associated with an assembly. + +' Review the values of the assembly attributes + + + + + + + + + +'In order to begin building localizable applications, set +'CultureYouAreCodingWith in your .vbproj file +'inside a . For example, if you are using US english +'in your source files, set the to "en-US". Then uncomment the +'NeutralResourceLanguage attribute below. Update the "en-US" in the line +'below to match the UICulture setting in the project file. + +' + + +'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found. +'1st parameter: where theme specific resource dictionaries are located +'(used if a resource is not found in the page, +' or application resource dictionaries) + +'2nd parameter: where the generic resource dictionary is located +'(used if a resource is not found in the page, +'app, and any theme specific resource dictionaries) + + + + +'The following GUID is for the ID of the typelib if this project is exposed to COM + + +' Version information for an assembly consists of the following four values: +' +' Major Version +' Minor Version +' Build Number +' Revision +' +' You can specify all the values or you can default the Build and Revision Numbers +' by using the '*' as shown below: +' + + + diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/MyExtensions/MyWpfExtension.vb b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/MyExtensions/MyWpfExtension.vb new file mode 100644 index 0000000000..22f84b7da5 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/MyExtensions/MyWpfExtension.vb @@ -0,0 +1,121 @@ +#If _MyType <> "Empty" Then + +Namespace My + ''' + ''' Module used to define the properties that are available in the My Namespace for WPF + ''' + ''' + _ + Module MyWpfExtension + Private s_Computer As New ThreadSafeObjectProvider(Of Global.Microsoft.VisualBasic.Devices.Computer) + Private s_User As New ThreadSafeObjectProvider(Of Global.Microsoft.VisualBasic.ApplicationServices.User) + Private s_Windows As New ThreadSafeObjectProvider(Of MyWindows) + Private s_Log As New ThreadSafeObjectProvider(Of Global.Microsoft.VisualBasic.Logging.Log) + ''' + ''' Returns the application object for the running application + ''' + _ + Friend ReadOnly Property Application() As Application + Get + Return CType(Global.System.Windows.Application.Current, Application) + End Get + End Property + ''' + ''' Returns information about the host computer. + ''' + _ + Friend ReadOnly Property Computer() As Global.Microsoft.VisualBasic.Devices.Computer + Get + Return s_Computer.GetInstance() + End Get + End Property + ''' + ''' Returns information for the current user. If you wish to run the application with the current + ''' Windows user credentials, call My.User.InitializeWithWindowsUser(). + ''' + _ + Friend ReadOnly Property User() As Global.Microsoft.VisualBasic.ApplicationServices.User + Get + Return s_User.GetInstance() + End Get + End Property + ''' + ''' Returns the application log. The listeners can be configured by the application's configuration file. + ''' + _ + Friend ReadOnly Property Log() As Global.Microsoft.VisualBasic.Logging.Log + Get + Return s_Log.GetInstance() + End Get + End Property + + ''' + ''' Returns the collection of Windows defined in the project. + ''' + _ + Friend ReadOnly Property Windows() As MyWindows + _ + Get + Return s_Windows.GetInstance() + End Get + End Property + _ + _ + Friend NotInheritable Class MyWindows + _ + Private Shared Function Create__Instance__(Of T As {New, Global.System.Windows.Window})(ByVal Instance As T) As T + If Instance Is Nothing Then + If s_WindowBeingCreated IsNot Nothing Then + If s_WindowBeingCreated.ContainsKey(GetType(T)) = True Then + Throw New Global.System.InvalidOperationException("The window cannot be accessed via My.Windows from the Window constructor.") + End If + Else + s_WindowBeingCreated = New Global.System.Collections.Hashtable() + End If + s_WindowBeingCreated.Add(GetType(T), Nothing) + Return New T() + s_WindowBeingCreated.Remove(GetType(T)) + Else + Return Instance + End If + End Function + _ + _ + Private Sub Dispose__Instance__(Of T As Global.System.Windows.Window)(ByRef instance As T) + instance = Nothing + End Sub + _ + _ + Public Sub New() + MyBase.New() + End Sub + Private Shared s_WindowBeingCreated As Global.System.Collections.Hashtable + Public Overrides Function Equals(ByVal o As Object) As Boolean + Return MyBase.Equals(o) + End Function + Public Overrides Function GetHashCode() As Integer + Return MyBase.GetHashCode + End Function + _ + _ + Friend Overloads Function [GetType]() As Global.System.Type + Return GetType(MyWindows) + End Function + Public Overrides Function ToString() As String + Return MyBase.ToString + End Function + End Class + End Module +End Namespace +Partial Class Application + Inherits Global.System.Windows.Application + _ + _ + Friend ReadOnly Property Info() As Global.Microsoft.VisualBasic.ApplicationServices.AssemblyInfo + _ + Get + Return New Global.Microsoft.VisualBasic.ApplicationServices.AssemblyInfo(Global.System.Reflection.Assembly.GetExecutingAssembly()) + End Get + End Property +End Class +#End If \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Resources.Designer.vb b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Resources.Designer.vb new file mode 100644 index 0000000000..e7ecddcab6 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Resources.Designer.vb @@ -0,0 +1,62 @@ +'------------------------------------------------------------------------------ +' +' This code was generated by a tool. +' Runtime Version:$clrversion$ +' +' Changes to this file may cause incorrect behavior and will be lost if +' the code is regenerated. +' +'------------------------------------------------------------------------------ + +Option Strict On +Option Explicit On + + +Namespace My.Resources + + 'This class was auto-generated by the StronglyTypedResourceBuilder + 'class via a tool like ResGen or Visual Studio. + 'To add or remove a member, edit your .ResX file then rerun ResGen + 'with the /str option, or rebuild your VS project. + ''' + ''' A strongly-typed resource class, for looking up localized strings, etc. + ''' + _ + Friend Module Resources + + Private resourceMan As Global.System.Resources.ResourceManager + + Private resourceCulture As Global.System.Globalization.CultureInfo + + ''' + ''' Returns the cached ResourceManager instance used by this class. + ''' + _ + Friend ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager + Get + If Object.ReferenceEquals(resourceMan, Nothing) Then + Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("$safeprojectname$.Resources", GetType(Resources).Assembly) + resourceMan = temp + End If + Return resourceMan + End Get + End Property + + ''' + ''' Overrides the current thread's CurrentUICulture property for all + ''' resource lookups using this strongly typed resource class. + ''' + _ + Friend Property Culture() As Global.System.Globalization.CultureInfo + Get + Return resourceCulture + End Get + Set(ByVal value As Global.System.Globalization.CultureInfo) + resourceCulture = value + End Set + End Property + End Module +End Namespace diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Resources.resx b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Resources.resx new file mode 100644 index 0000000000..af7dbebbac --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Settings.Designer.vb b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Settings.Designer.vb new file mode 100644 index 0000000000..51901f82e0 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Settings.Designer.vb @@ -0,0 +1,73 @@ +'------------------------------------------------------------------------------ +' +' This code was generated by a tool. +' Runtime Version:4.0.30319.42000 +' +' Changes to this file may cause incorrect behavior and will be lost if +' the code is regenerated. +' +'------------------------------------------------------------------------------ + +Option Strict On +Option Explicit On + + +Namespace My + + _ + Partial Friend NotInheritable Class MySettings + Inherits Global.System.Configuration.ApplicationSettingsBase + + Private Shared defaultInstance As MySettings = CType(Global.System.Configuration.ApplicationSettingsBase.Synchronized(New MySettings), MySettings) + +#Region "My.Settings Auto-Save Functionality" +#If _MyType = "WindowsForms" Then + Private Shared addedHandler As Boolean + + Private Shared addedHandlerLockObject As New Object + + _ + Private Shared Sub AutoSaveSettings(ByVal sender As Global.System.Object, ByVal e As Global.System.EventArgs) + If My.Application.SaveMySettingsOnExit Then + My.Settings.Save() + End If + End Sub +#End If +#End Region + + Public Shared ReadOnly Property [Default]() As MySettings + Get + +#If _MyType = "WindowsForms" Then + If Not addedHandler Then + SyncLock addedHandlerLockObject + If Not addedHandler Then + AddHandler My.Application.Shutdown, AddressOf AutoSaveSettings + addedHandler = True + End If + End SyncLock + End If +#End If + Return defaultInstance + End Get + End Property + End Class +End Namespace + +Namespace My + + _ + Friend Module MySettingsProperty + + + Friend ReadOnly Property Settings() As Global.SDKSamples.My.MySettings + Get + Return Global.SDKSamples.My.MySettings.Default + End Get + End Property + End Module +End Namespace diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Settings.settings b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Settings.settings new file mode 100644 index 0000000000..40ed9fdbad --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/My Project/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/ProjectVB.vbproj b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/ProjectVB.vbproj new file mode 100644 index 0000000000..02add77835 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/ProjectVB.vbproj @@ -0,0 +1,134 @@ + + + + Debug + AnyCPU + {7D142ED4-D5D6-4680-BD9E-290CA944A377} + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{F184B08F-C81C-45F6-A57F-5ABD9991F28F} + WinExe + SDKSamples + ThreadExample + v4.8.1 + Custom + true + true + + + AnyCPU + true + full + true + true + true + bin\Debug\ + ThreadExample.xml + 41999,42016,42017,42018,42019,42020,42021,42022,42032,42036,42314 + + + AnyCPU + pdbonly + false + false + true + false + true + bin\Release\ + ThreadExample.xml + 41999,42016,42017,42018,42019,42020,42021,42022,42032,42036,42314 + + + On + + + Binary + + + Off + + + On + + + + + + + + + + 4.0 + + + + + + + + + MSBuild:Compile + Designer + + + MainWindow.xaml + + + Designer + MSBuild:Compile + + + Application.xaml + Code + + + + + + + + + + + + + + + + + + + + + + + + Code + + + Microsoft.VisualBasic.WPF.MyExtension + 1.0.0.0 + + + True + True + Resources.resx + + + True + Settings.settings + True + + + VbMyResourcesResXFileCodeGenerator + Resources.Designer.vb + My.Resources + + + SettingsSingleFileGenerator + Settings.Designer.vb + + + + + + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/snippets.5000.json b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/snippets.5000.json new file mode 100644 index 0000000000..da9ebf8da2 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/advanced/snippets/how-to-detect-when-the-enter-key-pressed/vb/snippets.5000.json @@ -0,0 +1,3 @@ +{ + "host": "visualstudio" +} diff --git a/dotnet-desktop-guide/framework/wpf/data/how-to-create-a-simple-binding.md b/dotnet-desktop-guide/framework/wpf/data/how-to-create-a-simple-binding.md index e7ba89cf68..4ad6fa7f25 100644 --- a/dotnet-desktop-guide/framework/wpf/data/how-to-create-a-simple-binding.md +++ b/dotnet-desktop-guide/framework/wpf/data/how-to-create-a-simple-binding.md @@ -1,28 +1,83 @@ --- -title: "How to: Create a Simple Binding" -description: Create a simple binding for your applications through this how-to example in Windows Presentation Foundation (WPF). -ms.date: "03/30/2017" +title: "How to create a data binding" +description: Create a simple binding for your applications through this how-to example in Windows Presentation Foundation (WPF) and .NET Framework. +ms.date: 07/24/2024 +dev_langs: + - "csharp" + - "vb" helpviewer_keywords: - "simple binding [WPF], creating" - "data binding [WPF], creating simple bindings" - "binding data [WPF], creating" ms.assetid: 69b80f72-6259-44cb-8294-5bdcebca1e08 +#customer intent: As a devleoper, I want to create a binding so that I can present information in a UI --- -# How to: Create a Simple Binding - -This example shows you how to create a simple . - -## Example - - In this example, you have a `Person` object with a string property named `PersonName`. The `Person` object is defined in the namespace called `SDKSample`. - - The highlighted line that contains the `` element in the following example instantiates the `Person` object with a `PersonName` property value of `Joe`. This is done in the `Resources` section and assigned an `x:Key`. - - [!code-xaml[SimpleBinding](~/samples/snippets/csharp/VS_Snippets_Wpf/SimpleBinding/CSharp/Page1.xaml?highlight=9,37)] - - The highlighted line that contains the `` element then binds the control to the `PersonName` property. As a result, the appears with the value "Joe". - -## See also + +# How to create a data binding + +This article describes how to create a binding XAML. The example uses a data object that represents an employee at a company. This data object is bound to a XAML window that uses `TextBlock` controls to list the employee's details. You'll create a UI that looks like the following image: + +:::image type="content" source="media/how-to-create-a-simple-binding/preview.png" alt-text="A WPF window that shows details about an employee, such as their first name, last name, title, hire date, and salary."::: + +To learn more about data binding, see [Data binding overview in WPF](data-binding-overview.md). + +## Create a data object + +In this example, an employee is used as the data object that the UI is bound to. + +1. Add a new class to your project and name it `Employee`. +1. Replace the code with the following snippet: + + :::code language="csharp" source="./snippets/how-to-create-a-simple-binding/csharp/Employee.cs"::: + :::code language="vb" source="./snippets/how-to-create-a-simple-binding/vb/Employee.vb"::: + +The employee data object is a simple class that describes an employee: + +- The first and last name of the employee. +- The date the employee was hired. +- The employee's company title. +- How much income the employee earns. + +## Bind to a data object + +The following XAML demonstrates using the `Employee` class as a data object. The root element's `DataContext` property is bound to a static resource declared in the XAML. The individual controls are bound to the properties of the `Employee`. + +1. Add a new **Window** to the project and name it `EmployeeView` +1. Replace the XAML with the following snippet: + + > [!IMPORTANT] + > The following snippet is taken from a C# project. If you're using Visual Basic, the `x:Class` should be declared without the `ArticleSample` namespace. You can see what the Visual Basic version looks like [here](https://github.com/dotnet/docs-desktop/blob/main/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/vb/EmployeeView.xaml). + + :::code language="xaml" source="./snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml" highlight="7-9,33-37,43"::: + +The namespace of the code won't match your project's namespace, unless you created a project named **ArticleSample**. You can copy and paste the `Window.Resources` and root element (`StackPanel`) into you're **MainWindow** if you created a new project. + +To better understand how the previous XAML is using data binding, consider the following points: + +- A XAML resource is used to create an instance of the `Employee` class. + + Typically the data object is passed to or associated with the Window. This example hardcodes the employee for demonstration purposes. + + :::code language="xaml" source="./snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml" range="6-10"::: + +- The root element (`StackPanel`) has its data context set to the hardcoded `Employee` instance. + + The child elements inherit their `DataContext` from the parent, unless explicitly set. + + :::code language="xaml" source="./snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml" range="11"::: + +- The properties of the `Employee` instance are bound to the `TextBlock` controls. + + The `Binding` doesn't specify a `BindingSource`, so the `DataContext` is used as the source. + + :::code language="xaml" source="./snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml" range="33-37"::: + +- A `TextBox` control is bound with `TwoWay` mode, allowing the `TextBox` to change the `Employee.Salary` property. + + :::code language="xaml" source="./snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml" range="43"::: + +## Related content - [Data Binding Overview](data-binding-overview.md) +- [How to: Create a Binding in Code](how-to-create-a-binding-in-code.md) - [How-to Topics](data-binding-how-to-topics.md) diff --git a/dotnet-desktop-guide/framework/wpf/data/media/how-to-create-a-simple-binding/preview.png b/dotnet-desktop-guide/framework/wpf/data/media/how-to-create-a-simple-binding/preview.png new file mode 100644 index 0000000000..e72a53db93 Binary files /dev/null and b/dotnet-desktop-guide/framework/wpf/data/media/how-to-create-a-simple-binding/preview.png differ diff --git a/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/App.xaml b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/App.xaml new file mode 100644 index 0000000000..58956dbc5e --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/App.xaml.cs b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/App.xaml.cs new file mode 100644 index 0000000000..21d326fe82 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/App.xaml.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace ArticleSample +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/Employee.cs b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/Employee.cs new file mode 100644 index 0000000000..3f992af8dd --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/Employee.cs @@ -0,0 +1,28 @@ +using System; +using System.ComponentModel; + +namespace ArticleSample +{ + public class Employee : INotifyPropertyChanged + { + private decimal _salary; + public event PropertyChangedEventHandler PropertyChanged; + + public string FirstName { get; set; } + public string LastName { get; set; } + public string Title { get; set; } + public DateTime HireDate { get; set; } + + public decimal Salary + { + get => _salary; + set + { + _salary = value; + + // Support TwoWay binding + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Salary))); + } + } + } +} diff --git a/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml new file mode 100644 index 0000000000..467ff5e175 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml.cs b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml.cs new file mode 100644 index 0000000000..8e21833f54 --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/EmployeeView.xaml.cs @@ -0,0 +1,27 @@ +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.Shapes; + +namespace ArticleSample +{ + /// + /// Interaction logic for EmployeeView.xaml + /// + public partial class EmployeeView : Window + { + public EmployeeView() + { + InitializeComponent(); + } + } +} diff --git a/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/MainWindow.xaml b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/MainWindow.xaml new file mode 100644 index 0000000000..e83dd43b2e --- /dev/null +++ b/dotnet-desktop-guide/framework/wpf/data/snippets/how-to-create-a-simple-binding/csharp/MainWindow.xaml @@ -0,0 +1,12 @@ + + +