LoginSignup
1
1

More than 5 years have passed since last update.

Visual Studio / WPF > ObjectDataProvider | sys:Enumを用いてListBoxにenum値を表示

Last updated at Posted at 2017-06-06
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2
MainWindow.xaml.cs
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 _170606_1417_sysEnum
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public enum ColorEnum
    {
        Black = 0,
        Brown,
        Red,
        Orange,
        Yellow,
        Green,
        Blue,
        Violet,
        Gray        
    }
}

MainWindow.xaml
<Window x:Class="_170606_1417_sysEnum.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:_170606_1417_sysEnum"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ObjectDataProvider ObjectType="{x:Type sys:Enum}" MethodName="GetValues"
                            x:Key="colorEnum">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:ColorEnum"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <ListBox x:Name="listBox1" ItemsSource="{Binding Source={StaticResource colorEnum}}"/>
    </Grid>
</Window>

2017-06-06_14h29_26.png

新プロジェクトから追加したのは以下の部分。

  • xmlns:sysの定義
    • sys:Enum使用のため
  • public enum ColorEnum定義
  • ObjectDataProvider タグとその子要素
  • ListBoxの追加

以下をもとにした。
https://code.msdn.microsoft.com/windowsdesktop/WPF-ComboBoxEnum-ddd26709

sys:EnumでQiita検索してもこちらの記事ともうひとつの記事しか出てこない。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1