2017-01-12 74 views
0

我在我的XAML中遇到了一個奇怪的錯誤。我在WPF上向設計人員添加了一個組合框。 我給了這個組合框2項,與設計師。我該如何解決? 該組合框被稱爲:cb_gender。該項目:男女XAML組合框錯誤WPF

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> 

,這是錯誤:

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?)

+1

由於在示例代碼中沒有任何'ComboBoxItem_Selected',因此您沒有顯示有問題的代碼。一般來說,如果您將事件處理程序分配給XAML中的事件,則文件後面的代碼必須具有相應的具有該名稱的事件處理程序。 –

回答

1

首先體檢

看看屬性窗口您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?)

當沒有綁定事件時會發生此錯誤。查找.cs中的ComboBoxItem_Selected事件存根。輸入事件名稱或雙擊屬性窗口項目會自動生成具有有效參數的事件存根,但是編碼時可能會意外刪除它。

enter image description here

如果有中沒有方法存根您.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. 

第二個參數是不同的,方式太不同了。請確保你有一個有效的論證你的事件。