mirror of
https://github.com/Stone-Red-Code/docs-desktop.git
synced 2026-09-07 23:43:19 +02:00
Add how to bind enum article (#1056)
* add how to article * Fix lint * Fix the code example * Add missing code * Update how-to-bind-to-an-enumeration.md * Update dotnet-desktop-guide/net/wpf/data/how-to-bind-to-an-enumeration.md Co-authored-by: Tom Dykstra <[email protected]> Co-authored-by: Tom Dykstra <[email protected]>
This commit is contained in:
co-authored by
Tom Dykstra
parent
dd902d2e9c
commit
3885aab154
+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)
|
||||
)]
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<Window x:Class="ArticleExample.BindEnum"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
||||
SizeToContent="WidthAndHeight"
|
||||
Title="Enum binding">
|
||||
<!--<Resources>-->
|
||||
<Window.Resources>
|
||||
<ObjectDataProvider x:Key="EnumDataSource"
|
||||
ObjectType="{x:Type sys:Enum}"
|
||||
MethodName="GetValues">
|
||||
<ObjectDataProvider.MethodParameters>
|
||||
<x:Type TypeName="HorizontalAlignment" />
|
||||
</ObjectDataProvider.MethodParameters>
|
||||
</ObjectDataProvider>
|
||||
</Window.Resources>
|
||||
<!--</Resources>-->
|
||||
<!--<Content>-->
|
||||
<StackPanel Width="300" Margin="10">
|
||||
<TextBlock>Choose the HorizontalAlignment value of the Button:</TextBlock>
|
||||
|
||||
<!--<ListBox>-->
|
||||
<ListBox Name="myComboBox" SelectedIndex="0"
|
||||
ItemsSource="{Binding Source={StaticResource EnumDataSource}}"/>
|
||||
<!--</ListBox>-->
|
||||
|
||||
<Button Content="I'm a button"
|
||||
HorizontalAlignment="{Binding ElementName=myComboBox, Path=SelectedItem}" />
|
||||
</StackPanel>
|
||||
<!--</Content>-->
|
||||
</Window>
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
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 BindEnum.xaml
|
||||
/// </summary>
|
||||
public partial class BindEnum : Window
|
||||
{
|
||||
public BindEnum()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
//<EnumGetValues>
|
||||
var enumDataSource = System.Enum.GetValues(typeof(System.Windows.HorizontalAlignment));
|
||||
//</EnumGetValues>
|
||||
}
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
<Window x:Class="ArticleExample.BindEnumFull"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
||||
SizeToContent="WidthAndHeight"
|
||||
Title="Enum binding">
|
||||
<Window.Resources>
|
||||
<ObjectDataProvider x:Key="EnumDataSource"
|
||||
ObjectType="{x:Type sys:Enum}"
|
||||
MethodName="GetValues">
|
||||
<ObjectDataProvider.MethodParameters>
|
||||
<x:Type TypeName="HorizontalAlignment" />
|
||||
</ObjectDataProvider.MethodParameters>
|
||||
</ObjectDataProvider>
|
||||
</Window.Resources>
|
||||
|
||||
<StackPanel Width="300" Margin="10">
|
||||
<TextBlock>Choose the HorizontalAlignment value of the Button:</TextBlock>
|
||||
|
||||
<ListBox Name="myComboBox" SelectedIndex="0"
|
||||
ItemsSource="{Binding Source={StaticResource EnumDataSource}}"/>
|
||||
|
||||
<Button Content="I'm a button"
|
||||
HorizontalAlignment="{Binding ElementName=myComboBox, Path=SelectedItem}" />
|
||||
</StackPanel>
|
||||
</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 BindEnumFull.xaml
|
||||
/// </summary>
|
||||
public partial class BindEnumFull : Window
|
||||
{
|
||||
public BindEnumFull()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<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="Examples">
|
||||
|
||||
<StackPanel Width="200" Margin="35">
|
||||
<Button Click="Button1_Click">Bind to enum</Button>
|
||||
</StackPanel>
|
||||
</Window>
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
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 Button1_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
new BindEnum().ShowDialog();
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ArticleExample
|
||||
{
|
||||
enum PrimaryColors
|
||||
{
|
||||
Red,
|
||||
Yellow,
|
||||
Blue
|
||||
}
|
||||
}
|
||||
+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="MainWindow.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)>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="BindEnum"
|
||||
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="BindEnum" Height="450" Width="800">
|
||||
<Grid>
|
||||
|
||||
</Grid>
|
||||
</Window>
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
Public Class BindEnum
|
||||
Public Sub Method1()
|
||||
'<EnumGetValues>
|
||||
Dim enumDataSource = System.Enum.GetValues(GetType(System.Windows.HorizontalAlignment))
|
||||
'</EnumGetValues>
|
||||
End Sub
|
||||
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>
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
Class MainWindow
|
||||
Public Sub Method1()
|
||||
'<EnumGetValues>
|
||||
Dim enumValues = System.Enum.GetValues(GetType(System.Windows.HorizontalAlignment))
|
||||
'</EnumGetValues>
|
||||
End Sub
|
||||
End Class
|
||||
Reference in New Issue
Block a user