简体   繁体   English

XAML ComboBox错误WPF

[英]XAML ComboBox Error WPF

I got a strange error in my XAML. 我的XAML出现了一个奇怪的错误。 I added a combobox with the designer on WPF. 我在WPF上为设计人员添加了一个组合框。 I gave this combobox 2 items, with the designer. 我和设计师一起给了这个组合框2件商品。 How do I fix this? 我该如何解决? The combobox is called: cb_gender. 该组合框称为:cb_gender。 The items: Male and Female 项目:男女

XAML: XAML:

<Window xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"  x:Class="HotelWPFGoedeVersie.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:HotelWPFGoedeVersie"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid Name="grid_mainwindow" HorizontalAlignment="Left" Height="321" Margin="0,0,0,-0.2" VerticalAlignment="Top" Width="518">
            <Grid Name="grid_logo" HorizontalAlignment="Left" Height="80" Margin="377,0,0,0" VerticalAlignment="Top" Width="141">
                <Border BorderThickness="1">
                    <Ellipse Fill="#00FF00" HorizontalAlignment="Left" Height="79" Stroke="Black" VerticalAlignment="Top" Width="90" Margin="29.2,0.2,0,-0.8"/>
                </Border>
            </Grid>
            <Line Fill="#FF0000" Name="redline" Stroke="Black" StrokeThickness="4" X1="0" X2="133.6" Y1="70" Y2="70" Margin="385,-29,0,259" />
            <GroupBox x:Name="gb_newreservation" Header="New Reservation" HorizontalAlignment="Left" Height="263" Margin="19,10,0,0" VerticalAlignment="Top" Width="311">
                <Grid Name ="grid_reservation" HorizontalAlignment="Left" Height="225" Margin="10,10,0,0" VerticalAlignment="Top" Width="282">
                    <Label x:Name="lbl_name" Content="Name:" HorizontalAlignment="Left" Margin="0,19,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.443,-0.789"/>
                    <TextBox x:Name="tb_name" HorizontalAlignment="Left" Height="23" Margin="121,23,0,0" TextWrapping="Wrap" Text="Name" VerticalAlignment="Top" Width="120"/>
                    <Label x:Name="lbl_address" Content="Address:" HorizontalAlignment="Left" Margin="0,58,0,0" VerticalAlignment="Top"/>
                    <TextBox x:Name="tb_address" HorizontalAlignment="Left" Height="22" Margin="121,62,0,0" TextWrapping="Wrap" Text="Address" VerticalAlignment="Top" Width="120"/>
                    <Label x:Name="lbl_room" Content="Room:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,147,0,0"/>
                    <Label x:Name="lbl_date" Content="Date:" HorizontalAlignment="Left" Margin="0,190,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.193,-0.07"/>
                    <DatePicker Name="dp_date"  HorizontalAlignment="Left" Margin="121,192,0,0" VerticalAlignment="Top" FirstDayOfWeek="Monday" Width="120"/>
                    <ComboBox x:Name="cb_room" HorizontalAlignment="Left" Margin="121,150,0,0" VerticalAlignment="Top" Width="120" SelectedIndex="0" IsEditable="True"/>
                    <Label x:Name="lbl_gender" Content="Gender:" HorizontalAlignment="Left" Margin="0,89,0,0" VerticalAlignment="Top"/>
                    <ComboBox x:Name="cb_gender" HorizontalAlignment="Left" Margin="121,92,0,0" VerticalAlignment="Top" Width="120" SelectedIndex="0" IsEditable="True">
                        <ComboBoxItem Content="Male" HorizontalAlignment="Left" Width="118.4" />
                        <ComboBoxItem Content="Female" HorizontalAlignment="Left" Width="118.4"/>
                    </ComboBox>
                    <DatePicker Name="dp_birthday" HorizontalAlignment="Left" Margin="121,121,0,0" VerticalAlignment="Top" Width="120"/>
                    <Label x:Name="lbl_birthday" Content="Day of Birth: " HorizontalAlignment="Left" Margin="0,121,0,0" VerticalAlignment="Top"/>

                </Grid>

            </GroupBox>
            <Button x:Name="btn_addreservation" Content="Add Reservation" HorizontalAlignment="Left" Margin="19,278,0,0" VerticalAlignment="Top" Width="103" Height="26" Click="btn_addreservation_Click"/>
            <Button x:Name="btn_deletereservation" Content="Delete Reservation" HorizontalAlignment="Left" Margin="218,278,0,0" VerticalAlignment="Top" Width="103" Height="26"/>
        </Grid>

    </Grid>
</Window>

and this is the error: 这是错误:

Error CS1061 'MainWindow' does not contain a definition for >'ComboBoxItem_Selected' and no extension method 'ComboBoxItem_Selected' >accepting a first argument of type 'MainWindow' could be found (are you missing >a using directive or an assembly reference?) 错误CS1061'MainWindow'不包含>'ComboBoxItem_Selected'的定义,并且找不到扩展方法'ComboBoxItem_Selected'>接受类型为'MainWindow'的第一个参数(您是否丢失了>使用指令或程序集引用?)

First Checkup 第一次检查

Take a look at property window on your ComboBoxItem 看看ComboBoxItem上的属性窗口

Error CS1061 'MainWindow' does not contain a definition for 'ComboBoxItem_Selected' and no extension method 'ComboBoxItem_Selected' >accepting a first argument of type 'MainWindow' could be found (are you missing >a using directive or an assembly reference?) 错误CS1061'MainWindow'不包含'ComboBoxItem_Selected'的定义,并且找不到扩展方法'ComboBoxItem_Selected'>可以接受类型为'MainWindow'的第一个参数(您是否丢失了>使用指令或程序集引用?)

This error happens when there's no binding event. 没有绑定事件时,会发生此错误。 Look for the ComboBoxItem_Selected event stub in your .cs . .cs寻找ComboBoxItem_Selected事件存根。 Typing event name or double-click on an item of property window automatically generates the event stub with valid parameters, but you might have accidently removed it while coding. 键入事件名称或双击属性窗口的项目会自动生成带有有效参数的事件存根,但是您在编码时可能不小心删除了它。

在此处输入图片说明

If there's no method stub in your .cs leave it blank like the image above. 如果您的.cs没有方法存根,则将其保留为空白,如上图所示。 But if you have the method stub, type the exact event name into the input box. 但是,如果您有方法存根,请在输入框中键入确切的事件名称。

Further checkup 进一步检查

If you have the event method stub in your codebehind and you correctly bound the method to the event, then it's time to take a close look at the method's parameters. 如果您的代码后面有事件方法存根,并且已正确地将方法绑定到事件,那么现在该仔细看一下方法的参数了。 Similar events in terms of user action can have totally different event argument. 就用户操作而言,类似的事件可能具有完全不同的事件参数。

For example. 例如。

// Click event
private void Click(object sender, RoutedEventArgs e) { ... }

// MouseDown event
private void MouseDown(object sender, MouseButtonEventArgs e) { ... }

// Users can fire both events above by mouse click.

the second parameter is different, way too different. 第二个参数是不同的,太不同了。 Please make sure you have a valid argument for your event. 请确保您为事件指定了有效的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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