0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio / WPF > コントロール > TreeView > C#スクリプトで定義した文字列を表示する

Last updated at Posted at 2017-04-25
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

@ WPF 4.5入門 by 大田一希さん
No.3589 / 9985

TreeViewコントロールに関して、C#スクリプト内でTreeViewItemを設定している例が紹介されていた。

徳川家の家計図で試してみた。

XAML
<Window x:Class="_170425_t1630_treeView.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:_170425_t1630_treeView"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView Name="TokugawaFamily">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate DataType="local:Person"
                                          ItemsSource="{Binding Children}">
                    <TextBlock Text="{Binding Name}"/>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>

        </TreeView>
    </Grid>
</Window>
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 _170425_t1630_treeView
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var layer3 = new List<Person>();
            layer3.Add(new Person { Name = "家光" });
            layer3.Add(new Person { Name = "忠長" });
            layer3.Add(new Person { Name = "和子" });
            var layer2 = new List<Person>();
            layer2.Add(new Person { Name = "信康" });
            layer2.Add(new Person { Name = "秀康" });
            layer2.Add(new Person { Name = "秀忠", Children = layer3 });
            var layer1 = new List<Person>();
            layer1.Add(new Person { Name = "家康", Children = layer2 });

            TokugawaFamily.ItemsSource = layer1;
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public List<Person> Children { get; set; }
    }
}

work.png

備考

TokugawaFamily.ItemsSource = layer1;

TokugawaFamilyが見えないバグ?のような症状に遭遇した。

ソリューションを新規から作り直したらバグは見られなくなった。

XAMLファイルを壊したか、csファイルを壊したかは不明だった。

おかしくなった場合、VSを再起動してソリューションを開きなおすことでも回避できた。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?