简体   繁体   中英

WPF InitializeComponent is not recognized

Completely new to WPF and following a Microsoft walkthrough which asked to replace the XML with the following:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Northwind Orders" Height="335" Width="425" 
        Name="OrdersWindow" Loaded="Window1_Loaded">
    <Grid Name="orderItemsGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="258*"/>
            <ColumnDefinition Width="161*"/>
        </Grid.ColumnDefinitions>
        <ComboBox DisplayMemberPath="OrderID" ItemsSource="{Binding}"
                  IsSynchronizedWithCurrentItem="true" 
                  Height="23" Margin="92,12,37.703,0" Name="comboBoxOrder" VerticalAlignment="Top"/>
        <DataGrid ItemsSource="{Binding Path=Order_Details}"  
                  CanUserAddRows="False" CanUserDeleteRows="False"  
                  Name="orderItemsDataGrid" Margin="34,46,34.4,49.6"
                  AutoGenerateColumns="False" Grid.ColumnSpan="2">
            <DataGrid.Columns>
                <DataGridTextColumn  Header="Product" Binding="{Binding ProductID, Mode=OneWay}" />
                <DataGridTextColumn  Header="Quantity" Binding="{Binding Quantity, Mode=TwoWay}" />
                <DataGridTextColumn  Header="Price" Binding="{Binding UnitPrice, Mode=TwoWay}" />
                <DataGridTextColumn  Header="Discount" Binding="{Binding Discount, Mode=TwoWay}" />
            </DataGrid.Columns>
        </DataGrid>
        <Label Height="28" Margin="34,12,0,0" Name="orderLabel" VerticalAlignment="Top" 
               HorizontalAlignment="Left" Width="65">Order:</Label>
        <StackPanel Name="Buttons" Orientation="Horizontal" HorizontalAlignment="Right" 
                    Height="40" Margin="0,261,22.4,4.6" Grid.ColumnSpan="2">
            <Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" 
                Name="buttonSave" VerticalAlignment="Bottom" Width="75" 
                    Click="buttonSaveChanges_Click">Save Changes
            </Button>
            <Button Height="23" Margin="0,0,12,12" 
                Name="buttonClose" VerticalAlignment="Bottom" Width="75" 
                    Click="buttonClose_Click">Close</Button>
        </StackPanel>
    </Grid>
</Window>

So I replaced it but now if I go to the file MainWindow.xmal.cs the code for InitializeComponent(); is being highlighted as not existing.

在此处输入图片说明

Why is this and how do I correct it?

You must match the x:Class="Window1" in XAML with code behind class class MainWindow .

Change the XAML from x:Class="Window1" to x:Class="NorthwindClient.MainWindow"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM