Content update - Property value inheritance (user story 1878471) (#1261)

* Add article, code, toc, and redirects

* Resolve vb project issues

* Update .openpublishing.redirection.json

Co-authored-by: Andy (Steve) De George <[email protected]>
This commit is contained in:
Tris Shores
2022-01-05 15:16:55 -08:00
committed by GitHub
co-authored by Andy De George
parent cf371fba4c
commit e990fbc9a6
18 changed files with 928 additions and 0 deletions
@@ -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 CodeSampleCsharp
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
@@ -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)
)]
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>
</Project>
@@ -0,0 +1,29 @@
<Window x:Class="CodeSampleCsharp.MainWindow"
xmlns:local="clr-namespace:CodeSampleCsharp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Property value inheritance" Height="200" Width="400" Background="Yellow">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="260*"></ColumnDefinition>
<ColumnDefinition Width="260*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--<XamlElementTree>-->
<Canvas x:Name="canvas1" Grid.Column="0" Margin="20" Background="Orange" AllowDrop="True">
<StackPanel Name="stackPanel1" Margin="20" Background="Green">
<Label Name="label1" Margin="20" Height="40" Width="40" Background="Blue"/>
</StackPanel>
</Canvas>
<!--</XamlElementTree>-->
<local:Canvas_AllowDropInheritDisabled x:Name="canvas11" Grid.Column="1" Margin="20" Background="Orange" AllowDrop="True">
<StackPanel Name="stackPanel11" Margin="20" Background="Green">
<Label Name="label11" Margin="20" Height="40" Width="40" Background="Blue"/>
</StackPanel>
</local:Canvas_AllowDropInheritDisabled>
</Grid>
</Window>
@@ -0,0 +1,253 @@
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace CodeSampleCsharp
{
/// <summary>
/// Interaction logic for MainWindow.xaml.
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Code tests.
TestAllowDropInheritanceInXaml();
TestAllowDropInheritanceInCode();
TestIsTransparentInheritanceInCode();
TestDependencyPropertyWrapper();
}
private void TestAllowDropInheritanceInXaml()
{
// Test enabled property value inheritance.
FrameworkPropertyMetadata fpm1 = (FrameworkPropertyMetadata)
AllowDropProperty.GetMetadata(typeof(Canvas));
Debug.Assert(fpm1.Inherits == true);
Debug.Assert(canvas1.AllowDrop == true);
Debug.Assert(stackPanel1.AllowDrop == true);
Debug.Assert(label1.AllowDrop == true);
// Test disabled property value inheritance.
FrameworkPropertyMetadata fpm11 = (FrameworkPropertyMetadata)
AllowDropProperty.GetMetadata(typeof(Canvas_AllowDropInheritDisabled));
Debug.Assert(fpm11.Inherits == false);
Debug.Assert(canvas11.AllowDrop == true);
Debug.Assert(stackPanel11.AllowDrop == false);
Debug.Assert(label11.AllowDrop == false);
}
private static void TestAllowDropInheritanceInCode()
{
//<CodeElementTree>
Canvas canvas2 = new()
{
AllowDrop = true
};
StackPanel stackPanel2 = new();
Label label2 = new();
canvas2.Children.Add(stackPanel2);
stackPanel2.Children.Add(label2);
//</CodeElementTree>
// Test enabled property value inheritance.
FrameworkPropertyMetadata fpm2 = (FrameworkPropertyMetadata)
AllowDropProperty.GetMetadata(typeof(Canvas));
Debug.Assert(fpm2.Inherits == true);
Debug.Assert(canvas2.AllowDrop == true);
Debug.Assert(stackPanel2.AllowDrop == true);
Debug.Assert(label2.AllowDrop == true);
// Test disabled property value inheritance.
Canvas_AllowDropInheritDisabled canvas3 = new()
{
AllowDrop = true
};
StackPanel stackPanel3 = new();
Label label3 = new();
canvas3.Children.Add(stackPanel3);
stackPanel3.Children.Add(label3);
FrameworkPropertyMetadata fpm3 = (FrameworkPropertyMetadata)
AllowDropProperty.GetMetadata(typeof(Canvas_AllowDropInheritDisabled));
Debug.Assert(fpm3.Inherits == false);
Debug.Assert(canvas3.AllowDrop == true);
Debug.Assert(stackPanel3.AllowDrop == false);
Debug.Assert(label3.AllowDrop == false);
}
private static void TestIsTransparentInheritanceInCode()
{
// Test enabled property value inheritance.
Canvas_IsTransparentInheritEnabled myCanvas1 = new()
{
IsTransparent = true
};
Canvas_IsTransparentInheritEnabled2 myCanvas2 = new();
Canvas_IsTransparentInheritEnabled3 myCanvas3 = new();
myCanvas1.Children.Add(myCanvas2);
myCanvas2.Children.Add(myCanvas3);
FrameworkPropertyMetadata fpm1 = (FrameworkPropertyMetadata)Canvas_IsTransparentInheritEnabled
.IsTransparentProperty.GetMetadata(typeof(Canvas_IsTransparentInheritEnabled));
Debug.Assert(fpm1.Inherits == true);
Debug.Assert(myCanvas1.IsTransparent == true);
Debug.Assert(myCanvas2.IsTransparent == true);
Debug.Assert(myCanvas3.IsTransparent == true);
// Test disabled property value inheritance.
Canvas_IsTransparentInheritDisabled myCanvas4 = new()
{
IsTransparent = true
};
Canvas_IsTransparentInheritDisabled2 myCanvas5 = new();
Canvas_IsTransparentInheritDisabled3 myCanvas6 = new();
myCanvas4.Children.Add(myCanvas5);
myCanvas5.Children.Add(myCanvas6);
FrameworkPropertyMetadata fpm2 = (FrameworkPropertyMetadata)Canvas_IsTransparentInheritDisabled
.IsTransparentProperty.GetMetadata(typeof(Canvas_IsTransparentInheritDisabled));
Debug.Assert(fpm2.Inherits == false);
Debug.Assert(myCanvas4.IsTransparent == true);
Debug.Assert(myCanvas5.IsTransparent == false);
Debug.Assert(myCanvas6.IsTransparent == false);
}
private static void TestDependencyPropertyWrapper()
{
// Test Canvas_IsTransparentInheritEnabled.
Canvas_IsTransparentInheritEnabled myCanvas1 = new();
Canvas_IsTransparentInheritEnabled2 myCanvas2 = new();
Canvas_IsTransparentInheritEnabled3 myCanvas3 = new();
// Test property wrapper.
myCanvas1.IsTransparent = true;
myCanvas2.IsTransparent = false;
myCanvas3.IsTransparent = true;
Debug.Assert(myCanvas1.IsTransparent == true);
Debug.Assert(myCanvas2.IsTransparent == false);
Debug.Assert(myCanvas3.IsTransparent == true);
// Test individual get/set accessors.
Canvas_IsTransparentInheritEnabled.SetIsTransparent(myCanvas1, false);
Canvas_IsTransparentInheritEnabled.SetIsTransparent(myCanvas2, true);
Canvas_IsTransparentInheritEnabled.SetIsTransparent(myCanvas3, false);
Debug.Assert(Canvas_IsTransparentInheritEnabled.GetIsTransparent(myCanvas1) == false);
Debug.Assert(Canvas_IsTransparentInheritEnabled.GetIsTransparent(myCanvas2) == true);
Debug.Assert(Canvas_IsTransparentInheritEnabled.GetIsTransparent(myCanvas3) == false);
// Test Canvas_IsTransparentInheritDisabled.
Canvas_IsTransparentInheritDisabled myCanvas4 = new();
Canvas_IsTransparentInheritDisabled2 myCanvas5 = new();
Canvas_IsTransparentInheritDisabled3 myCanvas6 = new();
// Test property wrapper.
myCanvas4.IsTransparent = true;
myCanvas5.IsTransparent = false;
myCanvas6.IsTransparent = true;
Debug.Assert(myCanvas4.IsTransparent == true);
Debug.Assert(myCanvas5.IsTransparent == false);
Debug.Assert(myCanvas6.IsTransparent == true);
// Test individual get/set accessors.
Canvas_IsTransparentInheritDisabled.SetIsTransparent(myCanvas4, false);
Canvas_IsTransparentInheritDisabled.SetIsTransparent(myCanvas5, true);
Canvas_IsTransparentInheritDisabled.SetIsTransparent(myCanvas6, false);
Debug.Assert(Canvas_IsTransparentInheritDisabled.GetIsTransparent(myCanvas4) == false);
Debug.Assert(Canvas_IsTransparentInheritDisabled.GetIsTransparent(myCanvas5) == true);
Debug.Assert(Canvas_IsTransparentInheritDisabled.GetIsTransparent(myCanvas6) == false);
}
}
public class Canvas_AllowDropInheritDisabled : Canvas
{
static Canvas_AllowDropInheritDisabled()
{
// Disable property value inheritance in a new property metadata object.
FrameworkPropertyMetadata frameworkPropertyMetadata = new();
frameworkPropertyMetadata.Inherits = false;
// Override existing property metadata.
AllowDropProperty.OverrideMetadata(typeof(Canvas_AllowDropInheritDisabled), frameworkPropertyMetadata);
}
}
//<RegisterAttachedPropertyWithValueInheritance>
public class Canvas_IsTransparentInheritEnabled : Canvas
{
// Register an attached dependency property with the specified
// property name, property type, owner type, and property metadata
// (default value is 'false' and property value inheritance is enabled).
public static readonly DependencyProperty IsTransparentProperty =
DependencyProperty.RegisterAttached(
name: "IsTransparent",
propertyType: typeof(bool),
ownerType: typeof(Canvas_IsTransparentInheritEnabled),
defaultMetadata: new FrameworkPropertyMetadata(
defaultValue: false,
flags: FrameworkPropertyMetadataOptions.Inherits));
// Declare a get accessor method.
public static bool GetIsTransparent(Canvas element)
{
return (bool)element.GetValue(IsTransparentProperty);
}
// Declare a set accessor method.
public static void SetIsTransparent(Canvas element, bool value)
{
element.SetValue(IsTransparentProperty, value);
}
// For convenience, declare a property wrapper with get/set accessors.
public bool IsTransparent
{
get => (bool)GetValue(IsTransparentProperty);
set => SetValue(IsTransparentProperty, value);
}
}
//</RegisterAttachedPropertyWithValueInheritance>
public class Canvas_IsTransparentInheritEnabled2 : Canvas_IsTransparentInheritEnabled
{
}
public class Canvas_IsTransparentInheritEnabled3 : Canvas_IsTransparentInheritEnabled
{
}
public class Canvas_IsTransparentInheritDisabled : Canvas
{
// Register an attached dependency property with the specified
// property name, property type, owner type, and property metadata
// (default value is 'false' and value inheritance is disabled).
public static readonly DependencyProperty IsTransparentProperty =
DependencyProperty.RegisterAttached(
name: "IsTransparent",
propertyType: typeof(bool),
ownerType: typeof(Canvas_IsTransparentInheritDisabled),
defaultMetadata: new FrameworkPropertyMetadata(
defaultValue: false));
// Declare a get accessor method.
public static bool GetIsTransparent(Canvas element)
{
return (bool)element.GetValue(IsTransparentProperty);
}
// Declare a set accessor method.
public static void SetIsTransparent(Canvas element, bool value)
{
element.SetValue(IsTransparentProperty, value);
}
// For convenience, declare a property wrapper with get/set accessors.
public bool IsTransparent
{
get => (bool)GetValue(IsTransparentProperty);
set => SetValue(IsTransparentProperty, value);
}
}
public class Canvas_IsTransparentInheritDisabled2 : Canvas_IsTransparentInheritDisabled
{
}
public class Canvas_IsTransparentInheritDisabled3 : Canvas_IsTransparentInheritDisabled
{
}
}
@@ -0,0 +1,63 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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.
// </auto-generated>
//------------------------------------------------------------------------------
namespace CodeSampleCsharp.Properties {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// 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.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CodeSampleCsharp.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
}
}
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
@@ -0,0 +1,9 @@
<Application x:Class="CodeSampleCsharp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CodeSampleCsharp"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>