Пример #1
0
        static Button()
        {
            StyleProperty.OverrideMetadata(typeof(Button), new PropertyMetadata <Style>(
                                               new Style(typeof(Button))
                                               .SetLocalValue(Button.TemplateProperty, ControlTemplate.Create <Button>(t =>
            {
                var cp = new ContentPresenter();
                BindingOperations.SetBinding(cp, ContentPresenter.ContentProperty, new Binding {
                    Path = "Content", Source = t, Mode = BindingMode.OneWay
                });
                BindingOperations.SetBinding(cp, ContentPresenter.ContentTemplateProperty, new Binding {
                    Path = "ContentTemplate", Source = t, Mode = BindingMode.OneWay
                });
                BindingOperations.SetBinding(cp, ContentPresenter.HorizontalAlignmentProperty, new Binding {
                    Path = "HorizontalContentAlignment", Source = t, Mode = BindingMode.OneWay
                });
                BindingOperations.SetBinding(cp, ContentPresenter.VerticalAlignmentProperty, new Binding {
                    Path = "VerticalContentAlignment", Source = t, Mode = BindingMode.OneWay
                });

                var root = new Border
                {
                    Child = cp
                };
                BindingOperations.SetBinding(root, Border.BorderThicknessProperty, new Binding {
                    Path = new PropertyPath("BorderThickness"), Source = t
                });
                BindingOperations.SetBinding(root, Border.BackgroundProperty, new Binding {
                    Path = new PropertyPath("Background"), Source = t
                });
                BindingOperations.SetBinding(root, Border.BorderBrushProperty, new Binding {
                    Path = new PropertyPath("BorderBrush"), Source = t
                });

                return(root);
            }))
                                               .SetLocalValue(Button.HorizontalContentAlignmentProperty, HorizontalAlignment.Center)
                                               .SetLocalValue(Button.VerticalContentAlignmentProperty, VerticalAlignment.Center)
                                               ));
        }
Пример #2
0
        static ContentControl()
        {
            ContentControl.TemplateProperty.OverrideMetadata(typeof(ContentControl),
                                                             new UIPropertyMetadata <ControlTemplate>(ControlTemplate.Create <ContentControl>(t =>
            {
                var cp = new ContentPresenter();
                BindingOperations.SetBinding(cp, ContentPresenter.ContentProperty, new Binding {
                    Path = "Content", Source = t, Mode = BindingMode.OneWay
                });
                BindingOperations.SetBinding(cp, ContentPresenter.ContentTemplateProperty, new Binding {
                    Path = "ContentTemplate", Source = t, Mode = BindingMode.OneWay
                });
                BindingOperations.SetBinding(cp, ContentPresenter.HorizontalAlignmentProperty, new Binding {
                    Path = "HorizontalContentAlignment", Source = t, Mode = BindingMode.OneWay
                });
                BindingOperations.SetBinding(cp, ContentPresenter.VerticalAlignmentProperty, new Binding {
                    Path = "VerticalContentAlignment", Source = t, Mode = BindingMode.OneWay
                });

                return(cp);
            })));
        }
Пример #3
0
        static ListBox()
        {
            ListBox.ItemContainerGeneratorActivatorProperty.OverrideMetadata(typeof(ListBox),
                                                                             new PropertyMetadata <Func <ItemsControl, ItemContainerGenerator> >(l => new ListBoxItemContainerGenerator((ListBox)l)));
            ListBox.StyleProperty.OverrideMetadata(typeof(ListBox), new FrameworkPropertyMetadata <Style>(
                                                       new Style(typeof(ListBox))
                                                       .SetLocalValue(ListBox.TemplateProperty, ControlTemplate.Create <ListBox>(t =>
            {
                var itemsHost = new ItemsPresenter();
                BindingOperations.SetBinding(itemsHost, ItemsPresenter.ItemsSourceProperty, new Binding {
                    Path = "ItemsSource", Source = t
                });
                BindingOperations.SetBinding(itemsHost, ItemsPresenter.ItemContainerGeneratorProperty, new Binding {
                    Path = "ItemContainerGenerator", Source = t
                });
                BindingOperations.SetBinding(itemsHost, ItemsPresenter.ItemsPanelTemplateProperty, new Binding {
                    Path = "ItemsPanelTemplate", Source = t
                });

                var root = new Border
                {
                    Child = itemsHost
                };

                return(root);
            }))));
        }