locked
MainWindow.Content = Canvas not doing what I expect RRS feed

  • Question

  • In a new C# WPF project, I created class WelcomeScreen : Canvas, added a TextBlock to it and made the background color different than in the MainWindow, then assigned the WelcomeScreen to the Content property of the MainWindow. When I run the program, I only see the MainWindow and not what I thought should be on it (WelcomeScreen Canvas with text).

    If I instead put a StackPanel on the MainWindow and use that as the container for the WelcomeScreen, adding the WelcomeScreen as a child of the StackPanel, I also only see the MainWindow when I run the program.

    I must not understand something fundamental about what I am trying to do. Any idea what it is?

    Friday, March 16, 2018 12:49 PM

Answers

  • Instead of a window create a UserControl and change the tag to Canvas.  You don't need any code in the .cs file.  I've tried it and it works.  Doing this with window is not the way to go.

    Lloyd Sheen

    • Marked as answer by GregJ7 Friday, March 16, 2018 4:54 PM
    Friday, March 16, 2018 2:56 PM

All replies

  • With out any code XAML/C# it is hard to help.  


    Lloyd Sheen

    Friday, March 16, 2018 1:49 PM
  • Can I include an attachment somehow? I had a .7z file with the project ready to go. :/

    • Edited by GregJ7 Friday, March 16, 2018 1:50 PM
    Friday, March 16, 2018 1:49 PM
  • Post your code in this thread.  Use the "Insert Code Block" button to the right of the HTML button to add the code.

    Lloyd Sheen

    Friday, March 16, 2018 1:51 PM
  • Here is the (rather underwhelming) code:

    <Window x:Class="TestApp.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"
            mc:Ignorable="d"
            Title="MainWindow" Height="393" Width="776"
            Background="DarkGreen" WindowStyle="None" ResizeMode="CanMinimize">
    </Window>
    
    	public partial class MainWindow : Window {
    		public MainWindow() {
    			InitializeComponent();
    			this.Content = new WelcomeScreen();
    		}
    	}
    

    	public partial class WelcomeScreen : Canvas {
    		// Parameterless constructor so the XAML for this class can be displayed in the IDE.
    		public WelcomeScreen() : base() {
    		} // method
    	} // class
    
    <Canvas x:Class="TestApp.WelcomeScreen"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          mc:Ignorable="d"
          d:DesignWidth="776" d:DesignHeight="393"
          Background="Black"
          Margin="1,1,1,1">
      <TextBlock Canvas.Left="8" Canvas.Top="8" Text="Welcome!" Width="100"/>
    </Canvas>
    Friday, March 16, 2018 1:57 PM
  • How is the file with the Canvas info defined?  

    Lloyd Sheen

    Friday, March 16, 2018 2:18 PM
  • I'm not exactly sure what you mean. I created the WelcomeScreen Canvas/XAML/.cs files by adding a new item ["Window (WPF)"] to the project then changing the XAML root tags to "Canvas" from "Window."

    The project target framework is for .NET 4.5.

    Friday, March 16, 2018 2:38 PM
  • It appears that there is no personal messaging in this forum. If you know how I can get the zipped up project to you, let me know.
    Friday, March 16, 2018 2:49 PM
  • Instead of a window create a UserControl and change the tag to Canvas.  You don't need any code in the .cs file.  I've tried it and it works.  Doing this with window is not the way to go.

    Lloyd Sheen

    • Marked as answer by GregJ7 Friday, March 16, 2018 4:54 PM
    Friday, March 16, 2018 2:56 PM
  • Thanks! While trying various Controls other than Canvas I ran into a situation where I needed to remove InitializeComponent() from the WelcomScreen constructor and, not understanding exactly what it does (just loads the WelcomeScreen XAML I think), I forgot to put it back.
    Friday, March 16, 2018 4:54 PM