https://github.com/tatsuyafujisaki/wpf-cheat-sheet
https://github.com/tatsuyafujisaki/wpf-cheat-sheet
cheat-sheet csharp wpf
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/tatsuyafujisaki/wpf-cheat-sheet
- Owner: tatsuyafujisaki
- Created: 2016-03-22T11:54:09.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2021-01-07T13:53:57.000Z (over 4 years ago)
- Last Synced: 2025-03-24T00:55:36.428Z (3 months ago)
- Topics: cheat-sheet, csharp, wpf
- Language: C#
- Homepage:
- Size: 147 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Note
* `ApplicationCommands` cannot be used in MVVM.
* `InputBinding.CommandParameter` is not `DependencyProperty` so cannot pass a UI.
* Use `Behavior` to access `KeyEventArgs` or `MouseEventArgs`.
* `KeyBinding` cannot access `KeyEventArgs`.
* `MouseBinding` cannot access `MouseEventArgs`.
* `DataTemplate` is how to display an object in GUI and analogous to `ToString()` in CLI.
* How to make the UI thread wait until a non-UI thread is done.
```csharp
await Task.Run(() => IamNonUiThread());
```# Best practices
* Omit `Grid.Row="0"` and `Grid.Column="0"` as they are default.
* Omit `Width="*"` on `ColumnDefinition` and `Height="*"` on `RowDefinition` as star is default.
* A rather than B
* Use `{Binding Property1}` rather than `{Binding Path=Property1}` as `Path=` is optional.
* Use attribute syntax rather than property element syntax
* For example, use `` rather than `Content1\`
* Bind the following method to `PreviewMouseLeftButtonDown` rather than `MouseLeftButtonDown`. `MouseLeftButtonDown` is never called.
```csharp
void CheckIfNewVersionAvailable(object sender, MouseButtonEventArgs e)
{
if (IsLatestVersion(...))
{
return;
}if (MessageBox.Show("New version is available. Restart?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
// Start the new version and close self.
}// Not relay to the Click event.
e.Handled = true;
}
```# Glossary
Name|Description
---|---
Binding source|object
Binding target|UI# How to bind with self
```xml
"{Binding RelativeSource={RelativeSource Self}}"
```# How to bind with another UI
```xml```
# References
* [Binding Sources Overview](https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/binding-sources-overview)
* [Binding Mode Enum](https://docs.microsoft.com/en-us/dotnet/api/system.windows.data.bindingmode)