{"id":816,"date":"2021-02-14T18:05:26","date_gmt":"2021-02-14T09:05:26","guid":{"rendered":"https:\/\/todosoft.net\/blog\/?p=816"},"modified":"2021-02-14T18:05:27","modified_gmt":"2021-02-14T09:05:27","slug":"post-816","status":"publish","type":"post","link":"https:\/\/todosoft.net\/blog\/?p=816","title":{"rendered":"TreeView\u306e\u591a\u968e\u5c64\u3092Bind\u3067\u5b9f\u88c5\u3001\u30b9\u30bf\u30a4\u30eb\u3092\u6307\u5b9a\u3059\u308b"},"content":{"rendered":"\n<ul class=\"wp-block-list\"><li>TreeView \u306e ItemsSource \u306b\u5168\u4f53\u306e\u914d\u5217\u3092Bind\u3057\u3001 HierarchicalDataTemplate \u306e ItemsSource \u306b\u3001\u5b50\u8981\u7d20\u3092Bind\u3059\u308b<\/li><li>\u5404\u9805\u76ee\u306e\u30b9\u30bf\u30a4\u30eb\u306f\u3001TreeView.ItemContainerStyle \u306b\u8a2d\u5b9a\u3059\u308b<\/li><li>\u5b50\u8981\u7d20\u304c1\u3064\u3067\u3082\u3042\u308c\u3070\u3001\u5c55\u958b\uff08\uff1e\uff09\u30dc\u30bf\u30f3\u304c\u8868\u793a\u3055\u308c\u308b<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2021\/02\/TreeView.png\" alt=\"\" class=\"wp-image-821\" srcset=\"https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2021\/02\/TreeView.png 786w, https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2021\/02\/TreeView-300x169.png 300w, https:\/\/todosoft.net\/blog\/wp-content\/uploads\/2021\/02\/TreeView-768x433.png 768w\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">MainWindow.xaml<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">&lt;Window x:Class=\"WpfApp7.MainWindow\"\n        xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\n        xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\n        xmlns:d=\"http:\/\/schemas.microsoft.com\/expression\/blend\/2008\"\n        xmlns:mc=\"http:\/\/schemas.openxmlformats.org\/markup-compatibility\/2006\"\n        xmlns:local=\"clr-namespace:WpfApp7\"\n        mc:Ignorable=\"d\"\n        Title=\"MainWindow\" Height=\"450\" Width=\"800\">\n    \n    &lt;Grid>\n\n        &lt;TreeView ItemsSource=\"{Binding Mode=OneWay}\">\n            &lt;TreeView.Style>\n                &lt;!-- TreeView\u81ea\u4f53\u306e\u30b9\u30bf\u30a4\u30eb -->\n                &lt;Style TargetType=\"{x:Type TreeView}\" BasedOn=\"{StaticResource MaterialDesignTreeView}\">\n                    &lt;Setter Property=\"Background\" Value=\"#FFF0F0F0\"\/>\n                &lt;\/Style>\n            &lt;\/TreeView.Style>\n\n            &lt;TreeView.ItemContainerStyle>\n                &lt;!-- TreeView\u306e\u5404\u9805\u76ee\u306e\u30b9\u30bf\u30a4\u30eb -->\n                &lt;Style TargetType=\"{x:Type TreeViewItem}\" BasedOn=\"{StaticResource MaterialDesignTreeViewItem}\">\n                    &lt;Setter Property=\"Padding\" Value=\"0\"\/>\n                    &lt;Setter Property=\"Background\" Value=\"{Binding Path=BackColor, Mode=OneTime}\"\/>\n                &lt;\/Style>\n            &lt;\/TreeView.ItemContainerStyle>\n\n            &lt;TreeView.ItemTemplate>\n                &lt;!-- HierarchicalDataTemplate \u306e ItemSource \u306b\u3001\u5b50\u968e\u5c64\u3092Bind -->\n                &lt;HierarchicalDataTemplate DataType=\"local:TreeDataStructure\" ItemsSource=\"{Binding Path=SubData, Mode=OneWay}\">\n                    &lt;Label Content=\"{Binding Path=LabelTest, Mode=OneWay}\" Foreground=\"{Binding Path=ForeColor, Mode=OneTime}\"\/>\n                &lt;\/HierarchicalDataTemplate>\n            &lt;\/TreeView.ItemTemplate>\n        &lt;\/TreeView>\n        \n    &lt;\/Grid>\n    \n&lt;\/Window>\n<\/code><\/pre>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">MainWindow.xaml.cs<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">using System;\nusing System.Collections.Generic;\nusing System.Windows;\nusing System.Windows.Media;\n\nnamespace WpfApp7\n{\n    \/\/\/ &lt;summary>\n    \/\/\/ MainWindow.xaml \u306e\u76f8\u4e92\u4f5c\u7528\u30ed\u30b8\u30c3\u30af\n    \/\/\/ &lt;\/summary>\n    public partial class MainWindow : Window\n    {\n        private Random objRandom = new Random();\n\n        public MainWindow()\n        {\n            InitializeComponent();\n\n            CreateData();\n        }\n\n        private void CreateData()\n        {\n            List&lt;TreeDataStructure> ArrayTreeData = new List&lt;TreeDataStructure>();\n\n            for (int i = 0; i &lt; 10; i++)\n            {\n                TreeDataStructure objFirstTreeData = new TreeDataStructure();\n                objFirstTreeData.LabelTest = string.Format(\"{0}\", i);\n\n                byte f_r = GetRandomByte();\n                byte f_g = GetRandomByte();\n                byte f_b = GetRandomByte();\n                objFirstTreeData.ForeColor = new SolidColorBrush(Color.FromArgb(255, f_r, f_g, f_b));\n\n                byte b_r = GetRandomByte();\n                byte b_g = GetRandomByte();\n                byte b_b = GetRandomByte();\n                objFirstTreeData.BackColor = new SolidColorBrush(Color.FromArgb(128, b_r, b_g, b_b));\n\n                for (int j = 0; j &lt; 5; j++)\n                {\n                    TreeDataStructure objSecondTreeData = new TreeDataStructure();\n                    objSecondTreeData.LabelTest = string.Format(\"{0}-{1}\", i, j);\n                    objSecondTreeData.ForeColor = new SolidColorBrush(Color.FromArgb(GetRandomByte(), f_r, f_g, f_b));\n                    objSecondTreeData.BackColor = new SolidColorBrush(Color.FromArgb(GetRandomByte(), b_r, b_g, b_b));\n                    objFirstTreeData.SubData.Add(objSecondTreeData);\n                }\n\n                ArrayTreeData.Add(objFirstTreeData);\n            }\n\n            this.DataContext = ArrayTreeData;\n        }\n\n        private byte GetRandomByte()\n        {\n            return BitConverter.GetBytes(objRandom.Next(256))[0];\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">TreeDataStructure.cs<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">using System.Collections.Generic;\nusing System.Windows.Media;\n\nnamespace WpfApp7\n{\n    public class TreeDataStructure\n    {\n        public string LabelTest { get; set; } = \"\";\n        public List&lt;TreeDataStructure> SubData { get; set; } = new List&lt;TreeDataStructure>();\n        public Brush ForeColor { get; set; } = new SolidColorBrush(Colors.Black);\n        public Brush BackColor { get; set; } = new SolidColorBrush(Colors.White);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">App.xaml<\/h2>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">&lt;Application x:Class=\"WpfApp7.App\"\n             xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\n             xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\n             xmlns:local=\"clr-namespace:WpfApp7\"\n             xmlns:materialDesign=\"http:\/\/materialdesigninxaml.net\/winfx\/xaml\/themes\"\n             StartupUri=\"MainWindow.xaml\">\n    &lt;Application.Resources>\n\n        &lt;ResourceDictionary>\n            &lt;ResourceDictionary.MergedDictionaries>\n\n                &lt;!--MahApps-->\n                &lt;ResourceDictionary Source=\"pack:\/\/application:,,,\/MahApps.Metro;component\/Styles\/Controls.xaml\" \/>\n                &lt;ResourceDictionary Source=\"pack:\/\/application:,,,\/MahApps.Metro;component\/Styles\/Fonts.xaml\" \/>\n                &lt;ResourceDictionary Source=\"pack:\/\/application:,,,\/MahApps.Metro;component\/Styles\/Themes\/Light.Steel.xaml\" \/>\n\n                &lt;!--Material Design-->\n                &lt;materialDesign:BundledTheme BaseTheme=\"Light\" PrimaryColor=\"Indigo\" SecondaryColor=\"Indigo\" \/>\n                &lt;ResourceDictionary Source=\"pack:\/\/application:,,,\/MaterialDesignThemes.Wpf;component\/Themes\/MaterialDesignTheme.Defaults.xaml\" \/>\n\n                &lt;!--Material Design: MahApps\u7d71\u5408\u8a2d\u5b9a -->\n                &lt;ResourceDictionary Source=\"pack:\/\/application:,,,\/MaterialDesignThemes.MahApps;component\/Themes\/MaterialDesignTheme.MahApps.Fonts.xaml\" \/>\n                &lt;ResourceDictionary Source=\"pack:\/\/application:,,,\/MaterialDesignThemes.MahApps;component\/Themes\/MaterialDesignTheme.MahApps.Flyout.xaml\" \/>\n\n            &lt;\/ResourceDictionary.MergedDictionaries>\n\n        &lt;\/ResourceDictionary>\n\n    &lt;\/Application.Resources>\n&lt;\/Application>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>TreeView \u306e ItemsSource \u306b\u5168\u4f53\u306e\u914d\u5217\u3092Bind\u3057\u3001 HierarchicalDataTemplate \u306e ItemsSource \u306b\u3001\u5b50\u8981\u7d20\u3092Bind\u3059\u308b \u5404\u9805\u76ee\u306e\u30b9\u30bf\u30a4\u30eb\u306f\u3001TreeView.It [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,20,15],"tags":[95],"class_list":["post-816","post","type-post","status-publish","format-standard","hentry","category-c","category-wpf","category-15","tag-treeview"],"_links":{"self":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=816"}],"version-history":[{"count":5,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/816\/revisions"}],"predecessor-version":[{"id":822,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/816\/revisions\/822"}],"wp:attachment":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}