Wpf 如何将枚举绑定到文本块

Wpf 如何将枚举绑定到文本块,wpf,Wpf,我正在实施一个键盘训练器。我使用TexBlock(其他元素不适合此任务)来分配键。如何将enum System.Windows.Input.Key的值绑定到此TexBlock的标记?以下是答案;无论你想要什么,都可以从中得到你所需要的 在这个例子中有一些东西可以帮助你。最后,我将包含整个main窗口.xaml以供查看,但会将其分解并在此处进行解释 首先,您需要在视图中引用适当的名称空间 xmlns:WindowsInput="clr-namespace:System.Windows.Input;

我正在实施一个键盘训练器。我使用TexBlock(其他元素不适合此任务)来分配键。如何将enum System.Windows.Input.Key的值绑定到此TexBlock的标记?

以下是答案;无论你想要什么,都可以从中得到你所需要的

在这个例子中有一些东西可以帮助你。最后,我将包含整个
main窗口.xaml
以供查看,但会将其分解并在此处进行解释

首先,您需要在视图中引用适当的名称空间

xmlns:WindowsInput="clr-namespace:System.Windows.Input;assembly=WindowsBase"
xmlns:System="clr-namespace:System;assembly=mscorlib"
然后创建一个
ObjectDataProvider
,用于调用类型上的方法以及更多。您将在这里看到它是如何工作的,但我建议您阅读更多关于它的内容,因为这并不是它的全部目的。然而;在本例中,您将看到它引用了我们要调用的方法名称和类型,并允许我们将参数传递给该方法。这让我们在xaml中完成这一切,而不是代码隐藏;这对我来说要好得多。这个想法不仅仅是为了让它像现在这样出现在你的视野中,而是为了简单

<ObjectDataProvider x:Key="GetKeyNames"
                    MethodName="GetNames"
                    ObjectType="{x:Type System:Enum}">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="WindowsInput:Key" />
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
这需要更多的解释,但考虑到这个tid位和下面完整的
main window.xaml,我相信您会了解情况并看到问题的答案。如果你需要更多的解释,我很乐意更新答案

<Window x:Class="Question_Answer_WPF_App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:WindowsInput="clr-namespace:System.Windows.Input;assembly=WindowsBase"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="800">

    <Window.Resources>
        <ObjectDataProvider x:Key="GetKeyNames"
                            MethodName="GetNames"
                            ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="WindowsInput:Key" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>

    <ScrollViewer>
        <ItemsControl ItemsSource="{Binding Source={StaticResource GetKeyNames}}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Green"
                            BorderThickness="1"
                            Width="100"
                            Height="20">
                        <TextBlock Text="{Binding}"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Center" />
                    </Border>

                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Window>

此示例如下所示:


我将向您发送代码,说明如何将枚举绑定到
文本块
,但我不确定您为什么要将其绑定到
标记
,也不确定您希望显示的外观如何。这个问题是直截了当的,但在UI世界中,一些布局逻辑会有所帮助,因为答案可能会有所不同。
<Window x:Class="Question_Answer_WPF_App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:WindowsInput="clr-namespace:System.Windows.Input;assembly=WindowsBase"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="800">

    <Window.Resources>
        <ObjectDataProvider x:Key="GetKeyNames"
                            MethodName="GetNames"
                            ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="WindowsInput:Key" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>

    <ScrollViewer>
        <ItemsControl ItemsSource="{Binding Source={StaticResource GetKeyNames}}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Green"
                            BorderThickness="1"
                            Width="100"
                            Height="20">
                        <TextBlock Text="{Binding}"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Center" />
                    </Border>

                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Window>