diff --git a/dotnet-desktop-guide/net/wpf/data/binding-declarations-overview.md b/dotnet-desktop-guide/net/wpf/data/binding-declarations-overview.md index 5674d5e..beda009 100644 --- a/dotnet-desktop-guide/net/wpf/data/binding-declarations-overview.md +++ b/dotnet-desktop-guide/net/wpf/data/binding-declarations-overview.md @@ -55,6 +55,7 @@ For more information about the different terms, see [XAML Syntax In Detail (.NET Another way to specify a binding is to set properties directly on a object in code, and then assign the binding to a property. The following example shows how to create a object in code. :::code language="csharp" source="./snippets/binding-declarations-overview/csharp/DataBindingCode.xaml.cs" id="SetBinding"::: +:::code language="vb" source="./snippets/binding-declarations-overview/vb/DataBindingCode.xaml.vb" id="SetBinding"::: The previous code set the following on the binding: @@ -65,6 +66,11 @@ The previous code set the following on the binding: When the object you're binding is a or a , you can call the `SetBinding` method on your object directly instead of using . For an example, see [How to: Create a Binding in Code](../../../framework/wpf/data/how-to-create-a-binding-in-code.md). +The previous example uses a simple data object type of `Person`. The following is the code for that object: + +:::code language="csharp" source="./snippets/binding-declarations-overview/csharp/Person.cs" id="Person"::: +:::code language="vb" source="./snippets/binding-declarations-overview/vb/Person.vb" id="Person"::: + ## Binding path syntax Use the property to specify the source value you want to bind to: diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/csharp/DataBindingCode.xaml.cs b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/csharp/DataBindingCode.xaml.cs index 298f131..b6e47ba 100644 --- a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/csharp/DataBindingCode.xaml.cs +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/csharp/DataBindingCode.xaml.cs @@ -23,9 +23,9 @@ namespace ArticleExample // private class NameConverter : IValueConverter { - private static NameConverter _instance; - public static NameConverter Instance => //_instance ??= new NameConverter(); - _instance ?? (_instance = new NameConverter()); + private static NameConverter _instance = new(); + + public static NameConverter Instance => _instance; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => $"[[ {value} ]]"; diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/csharp/Person.cs b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/csharp/Person.cs index 0cecaab..7332a5b 100644 --- a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/csharp/Person.cs +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/csharp/Person.cs @@ -2,9 +2,11 @@ namespace ArticleExample { + // class Person { public string Name { get; set; } public DateTime Birthdate { get; set; } } + // } diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/Application.xaml b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/Application.xaml new file mode 100644 index 0000000..136613d --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/Application.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/Application.xaml.vb b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/Application.xaml.vb new file mode 100644 index 0000000..f3e8ffd --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/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/net/wpf/data/snippets/binding-declarations-overview/vb/ArticleExampleVB.vbproj b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/ArticleExampleVB.vbproj new file mode 100644 index 0000000..15b9652 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/ArticleExampleVB.vbproj @@ -0,0 +1,22 @@ + + + + WinExe + net5.0-windows + ArticleExampleVB + true + + + + + + + + + + + + + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/AssemblyInfo.vb b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/AssemblyInfo.vb new file mode 100644 index 0000000..025ee72 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/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/net/wpf/data/snippets/binding-declarations-overview/vb/DataBindingCode.xaml b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/DataBindingCode.xaml new file mode 100644 index 0000000..c97976c --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/DataBindingCode.xaml @@ -0,0 +1,16 @@ + + + + + + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/DataBindingCode.xaml.vb b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/DataBindingCode.xaml.vb new file mode 100644 index 0000000..4667da2 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/DataBindingCode.xaml.vb @@ -0,0 +1,46 @@ +Imports System.Globalization + +Public Class DataBindingCode + + ' + Private Class NameConverter + Implements IValueConverter + + Private Shared _instance As New NameConverter + + Public Shared ReadOnly Instance As NameConverter = _instance + + Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert + Return $"[[ {value} ]]" + End Function + + Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack + Return value.ToString().Trim("[", "]", " ") + End Function + End Class + ' + + ' + Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) + + ' Make a new data source object + Dim personDetails As New Person() With { + .Name = "John", + .Birthdate = Date.Parse("2001-02-03") + } + + ' New binding object using the path of 'Name' for whatever source object is used + Dim nameBindingObject As New Binding("Name") + + ' Configure the binding + nameBindingObject.Mode = BindingMode.OneWay + nameBindingObject.Source = personDetails + nameBindingObject.Converter = NameConverter.Instance + nameBindingObject.ConverterCulture = New CultureInfo("en-US") + + ' Set the binding to a target object. The TextBlock.Name property on the NameBlock UI element + BindingOperations.SetBinding(NameBlock, TextBlock.TextProperty, nameBindingObject) + + End Sub + ' +End Class diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/MainWindow.xaml b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/MainWindow.xaml new file mode 100644 index 0000000..9fc7d3a --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/MainWindow.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/MainWindow.xaml.vb b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/MainWindow.xaml.vb new file mode 100644 index 0000000..603c598 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/MainWindow.xaml.vb @@ -0,0 +1,3 @@ +Class MainWindow + +End Class diff --git a/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/Person.vb b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/Person.vb new file mode 100644 index 0000000..74232c0 --- /dev/null +++ b/dotnet-desktop-guide/net/wpf/data/snippets/binding-declarations-overview/vb/Person.vb @@ -0,0 +1,8 @@ +' +Public Class Person + + Public Property Name As String + Public Property Birthdate As DateTime + +End Class +'