Ejemplo n.º 1
0
        /// <summary>
        ///     Config.ConfigurableObjectAttribute 属性のついたデータ型をプロパティに持つオブジェクトの型から,各プロパティに対応するTabItemを取得します.
        ///     TabControlにメソッドの戻り値を追加し,TabControlのDataContextに<paramref name="type" />型オブジェクトのインスタンスを設定すると,値を編集できるようになります.
        /// </summary>
        /// <param name="defaultMargin">コンポーネントの既定のマージン</param>
        /// <returns>属性にしたがって<code>Header</code>と<code>Content</code>が設定されたTabItem</returns>
        private static IEnumerable <TabItem> GenerateTabItems(Thickness defaultMargin)
        {
            // コントロールのインスタンス化
            foreach (PropertyInfo propertyInfo in typeof(GlobalConfig).GetProperties())
            {
                object[] configurables =
                    propertyInfo.PropertyType.GetCustomAttributes(typeof(ConfigurableObjectAttribute), true);
                if (configurables.Length > 0)
                {
                    // Configurableとしてマークされている
                    var item = new TabItem {
                        Header = ((ConfigurableObjectAttribute)configurables[0]).Title
                    };
                    UserControl content = ConfigTools.GenerateUserControl(propertyInfo.PropertyType, defaultMargin);
                    content.Margin = defaultMargin;
                    item.Content   = content;

                    // Binding の作成
                    var binding = new Binding(propertyInfo.Name)
                    {
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                    };
                    content.SetBinding(DataContextProperty, binding);

                    yield return(item);
                }
            }
        }