Associating XAML UI object with non-auto-created class?

GregJ7

Member
Joined
Nov 23, 2024
Messages
12
Programming Experience
10+
How do I associate a UI control declared in XAML with a class. From looking at the auto-generated code, a class for my Grid (class "MainWinGrid") does not exist. Similarly, the apparent way to add a class defined in code to a XAML-defined Grid is something like what is shown below (class MazeDisplay), but this gives the error that it is not in "clr-namespace:AppNamespace" (nor in "AppNamespace" if I change the local definition to that). What is the correct way?

C#:
<Window x:Class="AppNamespace.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:AppNamespace"
        xmlns:loc="AppNamespace"
        mc:Ignorable="d"
        Title="MainWindow" Height="1000" Width="1250" Background="Black">
    <Grid Name="MainWinGrid" Background="Transparent" Loaded="MainWinGrid_Loaded" SizeChanged="MainWinGrid_SizeChanged" MouseLeftButtonUp="MainWinGrid_MouseLeftButtonUp">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="4*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <local:MazeDisplay Background="Transparent">
        </local:MazeDisplay>
    </Grid>
</Window>

C#:
namespace AppNamespace
{
    public class MazeDisplay : System.Windows.Controls.Panel
    {
        public MazeDisplay() : base()
        {
            Debug.WriteLine("*** In MazeDisplay() Constructor.");
        }
    }
}
 
I think there is a demo of WPF app that essentially looks like a 3D engine rendering demonstration where the screen is actively redrawing the contents of the screen. Perhaps look into that to see how they structured the code for it.
 
Thanks for your help. It looks like the non-XAML approach is going to work. I found a YouTube video for getting it started (delete the .xaml and code-behind and add an entry point Main).
 
Back
Top Bottom