LoginSignup
0
1

More than 5 years have passed since last update.

Visual Studio / WPF > TreeView内での2つのプロパティ(例: OSとcodename)のBindingによる表示例 > MultiBinding StringFormat={}{0} {1}

Last updated at Posted at 2017-06-08
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

http://gushwell.ldblog.jp/archives/52334146.html
を元に学習中。

以下の変更をしてみた。

  • 自作クラスのNameプロパティに加えてCodeNameプロパティを追加
  • 2つのプロパティをあわせてTreeView内で表示

参考: https://stackoverflow.com/questions/23225751/wpf-binding-to-two-properties
adPartageさんの回答を参考にしました。

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;
//
using System.Collections.ObjectModel;

namespace _170608_t1820_treeview
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public class Category : ObservableCollection<Category>
    {
        public Category()
        {
        }
        public Category(string name, String codename)
        {
            Name = name;
            CodeName = codename;
        }
        public string Name { get; set; }
        public string CodeName { get; set; }
        public Category Children { get; set; }
    }
    public class OSTypes
    {
        public Category Categories { get; set; }
        public OSTypes()
        {
            Categories = new Category()
            {
                new Category("OS", "")
                {
                    Children = new Category
                    {
                        new Category("Windows", "")
                        {
                            Children = new Category
                            {
                                new Category("Windows 10", "Threshold"),
                                new Category("Windows 8.1", "Blue"),
                            }
                        },
                        new Category("Mac OS X", "")
                        {
                            Children = new Category
                            {
                                new Category("Mac OS X 10.11", "El Capitan")
                            }
                        }
                    }
                }
            };
        }
    }

}
MainWindow.xaml
<Window x:Class="_170608_t1820_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:_170608_t1820_treeview"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:OSTypes x:Key="keyOSTypes"/>
        <HierarchicalDataTemplate x:Key="keyTreeViewTemplate"
                                  ItemsSource="{Binding Path=Children}">
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0}({1})">
                        <Binding Path="Name"/>
                        <Binding Path="CodeName"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Window.DataContext>
        <Binding Mode="OneWay" Source="{StaticResource keyOSTypes}"/>
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TreeView ItemsSource="{Binding Categories}"
                  ItemTemplate="{StaticResource keyTreeViewTemplate}"/>
    </Grid>
</Window>

2017-06-08_19h19_00.png

snippet

        <HierarchicalDataTemplate x:Key="keyTreeViewTemplate"
                                  ItemsSource="{Binding Path=Children}">
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0}({1})">
                        <Binding Path="Name"/>
                        <Binding Path="CodeName"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </HierarchicalDataTemplate>

keyTreeViewTemplateの定義において、単純にTextBlockを2つ(Name, CodeName)並べるとエラーが出た。

プロパティ 'VisualTree'が複数回設定されています。
プロパティ 'VisualTree'は1回しか設定できません。

MultiBinding というのがあることを知り使ったらうまくいった。

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