【问题标题】:XAML ComboBox Error WPFXAML 组合框错误 WPF
【发布时间】:2017-05-28 06:07:28
【问题描述】:

我的 XAML 中有一个奇怪的错误。我在 WPF 上与设计师一起添加了一个组合框。 我和设计师一起给了这个组合框 2 个项目。我该如何解决? 组合框称为:cb_gender。项目:男性和女性

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>

这是错误:

错误 CS1061 'MainWindow' 不包含 >'ComboBoxItem_Selected' 的定义,并且没有扩展方法 'ComboBoxItem_Selected' >可以找到接受类型为“MainWindow”的第一个参数(您是否缺少 > using 指令或程序集引用?)

【问题讨论】:

  • 您没有显示有问题的代码,因为您的示例代码中的任何地方都没有ComboBoxItem_Selected。不过,一般来说,如果您在 XAML 中为事件分配事件处理程序,则文件背后的代码必须具有相应的事件处理程序,并具有该名称。

标签: c# wpf xaml combobox


【解决方案1】:

第一次检查

查看ComboBoxItem上的属性窗口

错误 CS1061 'MainWindow' 不包含 'ComboBoxItem_Selected' 并且没有扩展方法 'ComboBoxItem_Selected' >接受类型的第一个参数 可以找到“MainWindow”(您是否缺少 >a using 指令或 汇编参考?)

当没有绑定事件时会发生此错误。在.cs 中查找ComboBoxItem_Selected 事件存根。键入事件名称或双击属性窗口项会自动生成具有有效参数的事件存根,但您可能在编码时不小心将其删除。

如果您的.cs 中没有方法存根,请将其留空,如上图所示。但如果您有方法存根,请在输入框中输入确切的事件名称。

进一步检查

如果您的代码隐藏中有事件方法存根,并且您正确地将方法绑定到事件,那么是时候仔细查看方法的参数了。 用户操作方面的类似事件可以有完全不同的事件参数。

例如。

// 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.

第二个参数不一样,太不一样了。请确保您的活动有一个有效的论据。

【讨论】:

    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2020-02-14
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多