Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Visual Studio Designer shows empty window with custom ContentPropertyAttribute

$
0
0

My application has a lot of windows and most of them share some basic features. Because of that I extended theWindow class to create a base for all my windows.

Everything compiles and displays fine but the designer just shows an empty window when I use my window class.

I made a basic working example that can be easily used, my real window is much more complex but this shows the problem. Here is the code:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;

namespace WpfApplication1
{
    [ContentProperty("ContentElement")]
    public class MyWindow : Window
    {
        public ToolBar ToolBar { get; private set; }
        public StatusBar StatusBar { get; private set; }
        public Border ContentBorder { get; private set; }

        public UIElement ContentElement
        {
            get { return (UIElement)GetValue(ContentElementProperty); }
            set { SetValue(ContentElementProperty, value); }
        }
        public static readonly DependencyProperty ContentElementProperty = DependencyProperty.Register(
 "ContentElement", typeof(UIElement), typeof(MyWindow),
            new PropertyMetadata(null, (d, e) =>
             {
                 MyWindow w = (MyWindow)d;
                 w.ContentBorder.Child = (UIElement)e.NewValue;
             }));

        public MyWindow() : base()
        {
            ToolBar = new ToolBar();
            ToolBar.Height = 30;
            ToolBar.VerticalAlignment = VerticalAlignment.Top;

            StatusBar = new StatusBar();
            StatusBar.Height = 20;
            StatusBar.VerticalAlignment = VerticalAlignment.Bottom;

            ContentBorder = new Border();
            ContentBorder.SetValue(MarginProperty, new Thickness(0, 30, 0, 20));

            Grid grid = new Grid();
            grid.Children.Add(ToolBar);
            grid.Children.Add(ContentBorder);
            grid.Children.Add(StatusBar);
            Content = grid;
        }
    }
}

XAML:

<local:MyWindow x:Class="WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300"><Grid><Rectangle Fill="Blue" /></Grid></local:MyWindow>

Doing the exact same thing with a UserControl works just fine, also in the designer. Just replace every occurance ofMyWindow with MyUserControl and extend from UserControl if you want to try that.

Is there any way I can get a custom Window like that to work with the designer, or do i have to make aUserControl and use that in every window? Also, is this some kind of bug or intended behavior?

Addional info: I'm running Visual Studio 2015 Community and I'm using .net 4.6

I Also tried another approach. Instead of using the ContentPropertyAttribute i have overwritten the ContentProperty like this:

new public object Content {
    get { return GetValue(ContentProperty); }
    set { SetValue(ContentProperty, value); }
}
new public static DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(BaseUserControl), new PropertyMetadata(null, (s, e) =>
{
    MyWindow bw = (MyWindow)s;
    bw.ContentBorder.Child = (UIElement)e.NewValue;
}));
Again this works completely fine with a UserControl. With the Window I can at least see the Content in the designer, but the ToolBar and StatusBar are still not visible in the Designer. When running it everything works correctly



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>