Visual Studio / WPF > TreeView内での2つのプロパティ(例: OSとcodename)のBindingによる表示例 > MultiBinding StringFormat={}{0} {1}
12789 ワード
動作環境
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>
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 というのがあることを知り使ったらうまくいった。
Author And Source
この問題について(Visual Studio / WPF > TreeView内での2つのプロパティ(例: OSとcodename)のBindingによる表示例 > MultiBinding StringFormat={}{0} {1}), 我々は、より多くの情報をここで見つけました https://qiita.com/7of9/items/36fdc56a78761223a61e著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .