mirror of
https://github.com/Stone-Red-Code/docs-desktop.git
synced 2026-09-04 09:06:04 +02:00
Add vb code;person example (#1052)
This commit is contained in:
@@ -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 <xref:System.Windows.Data.Binding> object in code, and then assign the binding to a property. The following example shows how to create a <xref:System.Windows.Data.Binding> 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 <xref:System.Windows.FrameworkElement> or a <xref:System.Windows.FrameworkContentElement>, you can call the `SetBinding` method on your object directly instead of using <xref:System.Windows.Data.BindingOperations.SetBinding%2A?displayProperty=nameWithType>. 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 <xref:System.Windows.Data.Binding.Path%2A> property to specify the source value you want to bind to:
|
||||
|
||||
+3
-3
@@ -23,9 +23,9 @@ namespace ArticleExample
|
||||
// <Converter>
|
||||
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} ]]";
|
||||
|
||||
+2
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace ArticleExample
|
||||
{
|
||||
//<Person>
|
||||
class Person
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public DateTime Birthdate { get; set; }
|
||||
}
|
||||
//</Person>
|
||||
}
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="Application"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:ArticleExampleVB"
|
||||
StartupUri="DataBindingCode.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
Class Application
|
||||
|
||||
' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
|
||||
' can be handled in this file.
|
||||
|
||||
End Class
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<RootNamespace>ArticleExampleVB</RootNamespace>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Import Include="System.Windows" />
|
||||
<Import Include="System.Windows.Controls" />
|
||||
<Import Include="System.Windows.Data" />
|
||||
<Import Include="System.Windows.Documents" />
|
||||
<Import Include="System.Windows.Input" />
|
||||
<Import Include="System.Windows.Media" />
|
||||
<Import Include="System.Windows.Media.Imaging" />
|
||||
<Import Include="System.Windows.Navigation" />
|
||||
<Import Include="System.Windows.Shapes" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+11
@@ -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)
|
||||
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)>
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
<Window x:Class="DataBindingCode"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:ArticleExampleVB"
|
||||
mc:Ignorable="d"
|
||||
Title="DataBindingCode" Height="450" Width="800"
|
||||
Loaded="Window_Loaded">
|
||||
<Border>
|
||||
<StackPanel Width="300" Margin="35">
|
||||
<Label>Person name:</Label>
|
||||
<TextBlock x:Name="NameBlock" Margin="10,0,0,0" />
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</Window>
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
Imports System.Globalization
|
||||
|
||||
Public Class DataBindingCode
|
||||
|
||||
'<Converter>
|
||||
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
|
||||
'</Converter>
|
||||
|
||||
'<SetBinding>
|
||||
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
|
||||
'</SetBinding>
|
||||
End Class
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:ArticleExampleVB"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
<Grid>
|
||||
|
||||
</Grid>
|
||||
</Window>
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Class MainWindow
|
||||
|
||||
End Class
|
||||
@@ -0,0 +1,8 @@
|
||||
'<Person>
|
||||
Public Class Person
|
||||
|
||||
Public Property Name As String
|
||||
Public Property Birthdate As DateTime
|
||||
|
||||
End Class
|
||||
'</Person>
|
||||
Reference in New Issue
Block a user