mirror of
https://github.com/Stone-Red-Code/docs-desktop.git
synced 2026-09-07 07:56:07 +02:00
Port databinding articles to WPF 5.0 (#1049)
* Binding declarations article * Binding sources article * TOC/Index updates * Update redirects * Acro/bugs * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * fix bookmarks Co-authored-by: Genevieve Warren <[email protected]>
This commit is contained in:
co-authored by
Genevieve Warren
parent
f48d6c37c0
commit
4adbe51408
+9
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="ArticleExample.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:ArticleExample"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace ArticleExample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
+10
@@ -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)
|
||||
)]
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="ArticleExample.DataBindingCode"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="DataBinding in code" SizeToContent="WidthAndHeight"
|
||||
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>
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
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 ArticleExample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for DataBindingCode.xaml
|
||||
/// </summary>
|
||||
public partial class DataBindingCode : Window
|
||||
{
|
||||
// <Converter>
|
||||
private class NameConverter : IValueConverter
|
||||
{
|
||||
private static NameConverter _instance;
|
||||
public static NameConverter Instance => //_instance ??= new NameConverter();
|
||||
_instance ?? (_instance = new NameConverter());
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
|
||||
$"[[ {value} ]]";
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
|
||||
value.ToString().Trim('[', ']', ' ');
|
||||
}
|
||||
// </Converter>
|
||||
|
||||
public DataBindingCode()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
// <SetBinding>
|
||||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Make a new data source object
|
||||
var personDetails = new Person()
|
||||
{
|
||||
Name = "John",
|
||||
Birthdate = DateTime.Parse("2001-02-03")
|
||||
};
|
||||
|
||||
// New binding object using the path of 'Name' for whatever source object is used
|
||||
var nameBindingObject = 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);
|
||||
}
|
||||
// </SetBinding>
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
<Window x:Class="ArticleExample.ExampleBinding"
|
||||
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:ArticleExample"
|
||||
mc:Ignorable="d"
|
||||
SizeToContent="WidthAndHeight"
|
||||
Title="Simple Data Binding Sample">
|
||||
|
||||
<Window.Resources>
|
||||
<local:Person x:Key="myDataSource" Name="Joe" Birthdate="2000-02-03"/>
|
||||
<Style TargetType="{x:Type Label}">
|
||||
<Setter Property="FontSize" Value="12"/>
|
||||
</Style>
|
||||
<Style TargetType="{x:Type TextBox}">
|
||||
<Setter Property="Width" Value="100"/>
|
||||
<Setter Property="Height" Value="25"/>
|
||||
</Style>
|
||||
<Style TargetType="{x:Type TextBlock}">
|
||||
<Setter Property="Width" Value="100"/>
|
||||
<Setter Property="Height" Value="25"/>
|
||||
<Setter Property="Padding" Value="3"/>
|
||||
</Style>
|
||||
</Window.Resources>
|
||||
<Border Margin="5" BorderBrush="Aqua" BorderThickness="1" Padding="8" CornerRadius="3">
|
||||
<StackPanel Width="200" Margin="35">
|
||||
<Label>Enter a Name:</Label>
|
||||
<TextBox>
|
||||
<TextBox.Text>
|
||||
<Binding Source="{StaticResource myDataSource}"
|
||||
Path="PersonName"
|
||||
UpdateSourceTrigger="PropertyChanged"/>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<Label>The name you entered:</Label>
|
||||
<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=Name}"/>
|
||||
<Label>The name you entered:</Label>
|
||||
<TextBlock>
|
||||
<TextBlock.Text>
|
||||
<Binding Source="{StaticResource myDataSource}" Path="Name"/>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</Window>
|
||||
+27
@@ -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 ArticleExample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for ExampleBinding.xaml
|
||||
/// </summary>
|
||||
public partial class ExampleBinding : Window
|
||||
{
|
||||
public ExampleBinding()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
<Window x:Class="ArticleExample.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:ArticleExample"
|
||||
mc:Ignorable="d"
|
||||
SizeToContent="WidthAndHeight"
|
||||
Title="Simple Data Binding Sample">
|
||||
|
||||
<StackPanel Width="200" Margin="35">
|
||||
<Button Click="ShowDataBindingCode_Click">DataBinding in code</Button>
|
||||
<Button Click="ShowBasicDataBinding_Click">Basic XAML of databinding</Button>
|
||||
</StackPanel>
|
||||
</Window>
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
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.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace ArticleExample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void ShowDataBindingCode_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
new DataBindingCode().ShowDialog();
|
||||
}
|
||||
|
||||
private void ShowBasicDataBinding_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
new ExampleBinding().ShowDialog();
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace ArticleExample
|
||||
{
|
||||
class Person
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public DateTime Birthdate { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user