I
I
iRumba2015-06-04 14:08:30
WPF
iRumba, 2015-06-04 14:08:30

Why is the template not working?

there is a template in resourcedictionary

<Style TargetType="controls:NeoWindow">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="controls:NeoWindow">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Menu Name="mainMenu" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Top">
                            <MenuItem Header="sadf"/>

                        </Menu>
                        <TabControl Name="tabList" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

                        </TabControl>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

there is a window
<controls:NeoWindow x:Class="WpfApplication4.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300"
        xmlns:controls="clr-namespace:WpfApplication4" Loaded="NeoWindow_Loaded">
    <controls:NeoWindow.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/WpfApplication4;component/Dictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </controls:NeoWindow.Resources>
</controls:NeoWindow>

The problem is that the template is displayed normally in the designer, but at startup the window is empty.
And when you try to add a menu item with code
((Menu)this.Template.FindName("mainMenu", this)).Items.Add(new MenuItem() { Header = "qwe" });

a message is printed to the console
First step to handle exception of type 'System.NullReferenceException' in WpfApplication4.exe

It is strange that if the template is detached, then such an exception is not displayed in the console, but terminates the application with an error.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom, 2015-06-04
@iRumba

Because in TargetType you need to specify the type.

A
AxisPod, 2015-06-04
@AxisPod

Well so find where at you null is returned.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question