Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,14 @@
{
"source_path": "dotnet-desktop-guide/framework/wpf/properties/dependency-property-callbacks-and-validation.md",
"redirect_url": "/dotnet/desktop/wpf/advanced/dependency-property-callbacks-and-validation?view=netframeworkdesktop-4.8"
},
{
"source_path": "dotnet-desktop-guide/net/wpf/advanced/read-only-dependency-properties.md",
"redirect_url": "/dotnet/desktop/wpf/properties/read-only-dependency-properties?view=netdesktop-6.0"
},
{
"source_path": "dotnet-desktop-guide/framework/wpf/properties/read-only-dependency-properties.md",
"redirect_url": "/dotnet/desktop/wpf/advanced/read-only-dependency-properties?view=netframeworkdesktop-4.8"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: Create an Unbound DataGridView Control
title: How to Create an Unbound Windows Forms DataGridView Control

description: Populate an unbound Windows Forms DataGridView Control programmatically and display a small amount of data in a table format without binding it to a data source

ms.date: "03/30/2017"
dev_langs:
- "csharp"
Expand All @@ -11,18 +14,21 @@ helpviewer_keywords:
ms.assetid: b5d4b47d-9a28-4d88-9dba-0a3c90fba71d
---
# How to: Create an Unbound Windows Forms DataGridView Control
The following code example demonstrates how to populate a <xref:System.Windows.Forms.DataGridView> control programmatically without binding it to a data source. This is useful when you have a small amount of data that you want to display in a table format.

The following code example demonstrates how to populate a <xref:System.Windows.Forms.DataGridView> control programmatically without binding it to a data source. This is useful when you have a small amount of data that you want to display in a table format.

For a complete explanation of this code example, see [Walkthrough: Creating an Unbound Windows Forms DataGridView Control](walkthrough-creating-an-unbound-windows-forms-datagridview-control.md).
For a complete explanation of this code example, see [Walkthrough: Creating an Unbound Windows Forms DataGridView Control](walkthrough-creating-an-unbound-windows-forms-datagridview-control.md).

## Example

## Example
[!code-csharp[System.Windows.Forms.DataGridViewSimpleUnbound#00](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewSimpleUnbound/CS/simpleunbound.cs#00)]
[!code-vb[System.Windows.Forms.DataGridViewSimpleUnbound#00](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewSimpleUnbound/VB/simpleunbound.vb#00)]
[!code-vb[System.Windows.Forms.DataGridViewSimpleUnbound#00](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewSimpleUnbound/VB/simpleunbound.vb#00)]

## Compiling the Code
This example requires:
## Compiling the Code

This example requires:

- References to the System, System.Drawing, and System.Windows.Forms assemblies.
- References to the System, System.Drawing, and System.Windows.Forms assemblies.

## See also

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: Create an Unbound DataGridView Control
title: Walkthrough to Creating an Unbound Windows Forms DataGridView Control

description: You can create an Unbound Windows Forms DataGridView Control and display a small amount of data without binding it to a data source

ms.date: "03/30/2017"
dev_langs:
- "csharp"
Expand All @@ -13,13 +16,14 @@ helpviewer_keywords:
ms.assetid: 5a8d6afa-1b4b-4b24-8db8-501086ffdebe
---
# Walkthrough: Creating an Unbound Windows Forms DataGridView Control

You may frequently want to display tabular data that does not originate from a database. For example, you may want to show the contents of a two-dimensional array of strings. The <xref:System.Windows.Forms.DataGridView> class provides an easy and highly customizable way to display data without binding to a data source. This walkthrough shows how to populate a <xref:System.Windows.Forms.DataGridView> control and manage the addition and deletion of rows in "unbound" mode. By default, the user can add new rows. To prevent row addition, set the <xref:System.Windows.Forms.DataGridView.AllowUserToAddRows%2A> property is `false`.

To copy the code in this topic as a single listing, see [How to: Create an Unbound Windows Forms DataGridView Control](how-to-create-an-unbound-windows-forms-datagridview-control.md).

## Creating the Form

#### To use an unbound DataGridView control
### To use an unbound DataGridView control

1. Create a class that derives from <xref:System.Windows.Forms.Form> and contains the following variable declarations and `Main` method.

Expand Down Expand Up @@ -64,16 +68,18 @@ You may frequently want to display tabular data that does not originate from a d
[!code-csharp[System.Windows.Forms.DataGridViewSimpleUnbound#10](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewSimpleUnbound/CS/simpleunbound.cs#10)]
[!code-vb[System.Windows.Forms.DataGridViewSimpleUnbound#10](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.DataGridViewSimpleUnbound/VB/simpleunbound.vb#10)]

## Testing the Application
## Testing the Application

You can now test the form to make sure it behaves as expected.

#### To test the form
### To test the form

- Press F5 to run the application.

You will see a <xref:System.Windows.Forms.DataGridView> control that displays the songs listed in `PopulateDataGridView`. You can add new rows with the **Add Row** button, and you can delete selected rows with the **Delete Row** button. The unbound <xref:System.Windows.Forms.DataGridView> control is the data store, and its data is independent of any external source, such as a <xref:System.Data.DataSet> or an array.

## Next Steps
## Next Steps

This application gives you a basic understanding of the <xref:System.Windows.Forms.DataGridView> control's capabilities. You can customize the appearance and behavior of the <xref:System.Windows.Forms.DataGridView> control in several ways:

- Change border and header styles. For more information, see [How to: Change the Border and Gridline Styles in the Windows Forms DataGridView Control](change-the-border-and-gridline-styles-in-the-datagrid.md).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: "How-to: Alter the Typography of Text"
title: How to Alter the Typography of Text

description: To alter the telegraphy of text, set the telegraphy attribute. You can set the typographic property of text programmatically. You can see how altered and default telegraphy properties of text render on screen each with an example.

ms.date: "03/30/2017"
dev_langs:
- "csharp"
Expand All @@ -10,24 +13,27 @@ helpviewer_keywords:
ms.assetid: 19a3b49b-60a2-4c11-a786-e26b4c965588
---
# How-to: Alter the Typography of Text
The following example shows how to set the <xref:System.Windows.Documents.TextElement.Typography%2A> attribute, using <xref:System.Windows.Documents.Paragraph> as the example element.

The following example shows how to set the <xref:System.Windows.Documents.TextElement.Typography%2A> attribute, using <xref:System.Windows.Documents.Paragraph> as the example element.

## Example
[!code-xaml[TextElementSnippets#_TextElement_TypogXAML](~/samples/snippets/csharp/VS_Snippets_Wpf/TextElementSnippets/CSharp/Window1.xaml#_textelement_typogxaml)]
## Example 1: To show how altered and default typographic properties of text render on screen

[!code-xaml[TextElementSnippets#_TextElement_TypogXAML](~/samples/snippets/csharp/VS_Snippets_Wpf/TextElementSnippets/CSharp/Window1.xaml#_textelement_typogxaml)]

The following figure shows how this example renders.
The following figure shows how this example renders.

![Screenshot: Text with altered typography](./media/textelement-typog.png "TextElement_Typog")
![Screenshot: Text element with altered typography](./media/textelement-typog.png "TextElement_Typog")

In contrast, the following figure shows how a similar example with default typographic properties renders.
In contrast, the following figure shows how a similar example with default typographic properties renders.

![Screenshot: Text with altered typography](./media/textelement-typog-default.png "TextElement_Typog_Default")
![Screenshot: Text element with default typography](./media/textelement-typog-default.png "TextElement_Typog_Default")

## Example
The following example shows how to set the <xref:System.Windows.Controls.TextBox.Typography%2A> property programmatically.
## Example 2: To show how to set typographic property of text programmatically

The following example shows how to set the <xref:System.Windows.Controls.TextBox.Typography%2A> property programmatically.

[!code-csharp[TextElementSnippets#_TextElement_Typog](~/samples/snippets/csharp/VS_Snippets_Wpf/TextElementSnippets/CSharp/Window1.xaml.cs#_textelement_typog)]
[!code-vb[TextElementSnippets#_TextElement_Typog](~/samples/snippets/visualbasic/VS_Snippets_Wpf/TextElementSnippets/visualbasic/window1.xaml.vb#_textelement_typog)]
[!code-vb[TextElementSnippets#_TextElement_Typog](~/samples/snippets/visualbasic/VS_Snippets_Wpf/TextElementSnippets/visualbasic/window1.xaml.vb#_textelement_typog)]

## See also

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
title: "Read-only dependency properties"
description: Learn about dependency properties in Windows Presentation Foundation (WPF) and how to create a read-only dependency property.
ms.date: "11/29/2021"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "dependency properties [WPF], read-only"
- "read-only dependency properties [WPF]"
---
<!-- The acrolinx score was 94 on 11/29/2021-->

# Read-only dependency properties (WPF .NET)

You can use read-only dependency properties to prevent property values being set from outside your code. This article discusses existing read-only dependency properties and the scenarios and techniques for creating a custom read-only dependency property.

[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]

## Prerequisites

The article assumes a basic knowledge of dependency properties, and that you've read [Dependency properties overview](dependency-properties-overview.md). To follow the examples in this article, it helps if you're familiar with Extensible Application Markup Language (XAML) and know how to write WPF applications.

## Existing read-only dependency properties

Read-only dependency properties typically report state, and shouldn't be modifiable through a `public` accessor. For example, the Windows Presentation Foundation (WPF) framework implements the <xref:System.Windows.UIElement.IsMouseOver%2A> property as read-only because its value should only be determined by mouse input. If `IsMouseOver` allowed other inputs, its value might become inconsistent with mouse input. Though not settable through a `public` accessor, many existing read-only dependency properties have values determined by multiple inputs.

## Uses of read-only dependency properties

Read-only dependency properties aren't applicable in several scenarios where dependency properties normally offer a solution. Non-applicable scenarios include data binding, applying a style to a value, validation, animation, and inheritance. However, a read-only dependency property can be used as a property trigger in a style. For example, <xref:System.Windows.UIElement.IsMouseOver%2A> is commonly used to trigger changes to the background, foreground, or other visible property of a control when the mouse is over it. The WPF property system detects and reports changes in read-only dependency properties, thus supporting property trigger functionality. Read-only dependency properties are also useful when implementing a collection-type dependency property where only the collection elements need to be writeable, not the collection object itself. For more information, see [Collection-type dependency properties](collection-type-dependency-properties.md).

> [!NOTE]
> Only dependency properties, not regular common language runtime properties, can be used as property triggers in a style.

## Creating custom read-only dependency properties

Before creating a dependency property that's read-only, check the [non-applicable scenarios](#uses-of-read-only-dependency-properties).

The process of creating a read-only dependency property is in many ways similar to creating read-write dependency properties, with these distinctions:

- When registering your read-only property, call <xref:System.Windows.DependencyProperty.RegisterReadOnly%2A> instead of <xref:System.Windows.DependencyProperty.Register%2A>.

- When implementing the CLR property wrapper, make sure it doesn't have a public `set` accessor.

- `RegisterReadOnly` returns <xref:System.Windows.DependencyPropertyKey> instead of <xref:System.Windows.DependencyProperty>. Store the `DependencyPropertyKey` in a nonpublic class member.

You can determine the value of your read-only dependency property using whatever logic you choose. The recommended way to set the property value, either initially or as part of runtime logic, is to use the overload of <xref:System.Windows.DependencyObject.SetValue%2A> that accepts a parameter of type `DependencyPropertyKey`. Using `SetValue` is preferable to circumventing the property system and setting the backing field directly.

How and where you set the value of a read-only dependency property within your application will affect the access level you assign to the class member that stores the `DependencyPropertyKey`. If you only set the property value from within the class that registers the dependency property, you can use a `private` access modifier. For scenarios where the values of dependency properties affect each other, you can use paired <xref:System.Windows.PropertyChangedCallback> and <xref:System.Windows.CoerceValueCallback> callbacks to trigger value changes. For more information, see [Dependency property metadata](dependency-property-metadata.md).

If you need to change the value of a read-only dependency property from outside the class that registers it, you can use an `internal` access modifier for the `DependencyPropertyKey`. For example, you might call `SetValue` from an event handler in the same assembly. The following example defines an Aquarium class that calls `RegisterReadOnly` to create the read-only dependency property `FishCount`. The `DependencyPropertyKey` is assigned to an `internal static readonly` field, so that code in the same assembly can change the read-only dependency property value.

:::code language="csharp" source="./snippets/read-only-dependency-properties/csharp/MainWindow.xaml.cs" id="RegisterReadOnlyDependencyProperty":::
:::code language="vb" source="./snippets/read-only-dependency-properties/vb/MainWindow.xaml.vb" id="RegisterReadOnlyDependencyProperty":::

Because the WPF property system doesn't propagate the <xref:System.Windows.DependencyPropertyKey> outside your code, read-only dependency properties have better write security than read-write dependency properties. Use a read-only dependency property when you want to limit write-access to those who have a reference to the `DependencyPropertyKey`.

In contrast, the dependency property identifier for read-write dependency properties is accessible through the property system, no matter what access modifier you assign it. For more information, see [Dependency property security](/dotnet/desktop/wpf/advanced/dependency-property-security?view=netframeworkdesktop-4.8&preserve-view=true).

## See also

- [Dependency properties overview](dependency-properties-overview.md)
- [Implement a Dependency property](how-to-implement-a-dependency-property.md)
- [Custom dependency properties](custom-dependency-properties.md)
- [Collection-type dependency properties](collection-type-dependency-properties.md)
- [Dependency property security](/dotnet/desktop/wpf/advanced/dependency-property-security?view=netframeworkdesktop-4.8&preserve-view=true)
- [Styles and templates in WPF](/dotnet/desktop/wpf/controls/styles-templates-overview)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<RootNamespace>CodeSampleVb</RootNamespace>
<UseWPF>true</UseWPF>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Window x:Class="CodeSampleCsharp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:CodeSampleCsharp"
Title="Collection-Type Dependency Properties" Height="100" Width="400">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Collection-Type Dependency Properties" Height="100" Width="500">

<StackPanel>

<Button Click="InitializeAquariums">Set collection default value in class constructor</Button>
<Button Click="InitializeAquariums">Set collection default value in class constructor (Aquarium)</Button>
<Button Click="InitializeFreezableAquariums">Set collection default value in class constructor (FreezableCollection Aquarium)</Button>

</StackPanel>
</Window>
Loading