简体   繁体   English

无法在组合框C#(和XAML)中执行selectedIndexChanged

[英]Cannot do selectedIndexChanged in combobox c#(and xaml)

I was trying to let a user choose which table to display from my database and I heard the best way of doing so is using the "selectedIndexChanged" event so that when the user clicks on the desired table it immediately displays it. 我试图让用户从数据库中选择要显示的表,并且我听说这样做的最好方法是使用“ selectedIndexChanged”事件,以便当用户单击所需表时,它立即显示该表。 However, for some reason when I click on my comboBox and then events I dont see "selectedIndexChanged" as an option and while writing it manually is says : "The member "SelectedIndexChanged" is not recognized or is not accessible" this is my xaml code : 但是,由于某些原因,当我单击我的comboBox并发生事件时,我没有看到“ selectedIndexChanged”作为选项,而在手动编写时说:“成员“ SelectedIndexChanged”未被识别或无法访问”,这是我的xaml代码:

 <Window x:Class="WpfApp1.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:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="4*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Canvas Name="mycanvas" Background="LightBlue" Margin="0,0,34.4,-0.2">
        <DataGrid Name="g1">
        </DataGrid>
        <Label Name="l1" Content="" FontWeight="ExtraBold" FontSize="15" Foreground="{x:Null}" Canvas.Left="20"/>
    </Canvas>
    <Canvas Grid.Column="1">
        <Label Name="Instruction" Content="Choose a table" FontWeight="ExtraBold" FontSize="15" Canvas.Left="-23" Canvas.Top="62"/>
        <ComboBox SelectedIndexChanged="b1" Name="ComboBox1" FontWeight="Bold" FontSize="15" Canvas.Top="98" Width="135" Height="24" Canvas.Left="-31">
            <ComboBoxItem Foreground="#FF3FA4C5"  Name="classesTbl">classesTbl</ComboBoxItem>
            <ComboBoxItem Foreground="#FF3FA4C5" Name="gradesTbl">gradesTbl</ComboBoxItem>
            <ComboBoxItem Foreground="#FF3FA4C5" Name="studentsTbl">studentsTbl</ComboBoxItem>
            <ComboBoxItem Foreground="#FF3FA4C5" Name="subjectsTbl">subjectsTbl</ComboBoxItem>
            <ComboBoxItem Foreground="#FF3FA4C5" Name="subjectsTeachers">subjectsTeachers</ComboBoxItem>
            <ComboBoxItem Foreground="#FF3FA4C5" Name="TeachersTbl">TeachersTbl</ComboBoxItem>
        </ComboBox>
    </Canvas>
</Grid>

Does anyone know how to solve it or what I did wrong? 有谁知道如何解决它或我做错了什么?

The WPF ComboBox control ( MSDN ) does not have a SelectedIndexChanged event, you may be thinking of the WinForms one. WPF ComboBox控件( MSDN )没有SelectedIndexChanged事件,您可能会想到WinForms事件。

Beyond that; 除此之外; you should be using MVVM ( MVVM: Tutorial from start to finish? ) and not relying much (if at all) on control events. 您应该使用MVVM( MVVM:从头到尾的教程? ),而不要过多地(如果有的话)依赖控制事件。 In this case you could easily accomplish this by binding SelectedItem and in the view model property's setter update the collection the DataGrid 's ItemsSource is bound to. 在这种情况下,您可以通过绑定SelectedItem轻松完成此操作,并在视图模型属性的设置器中更新DataGridItemsSource绑定到的集合。

One final note; 最后一点; the use of Canvas there is almost certainly incorrect; 几乎可以肯定使用Canvas是不正确的; you should just stick with the Grid so you have nice resizing instead of absolute positioning. 您应该只使用Grid这样您就可以很好地调整大小,而不是绝对定位。

SelectionChanged property can be used for a combo box and a command can be bound to it. SelectionChanged属性可以用于组合框,并且可以将命令绑定到该组合框。 The corresponding action can be written in the Viewmodel against the command 可以根据命令在Viewmodel中编写相应的动作

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

相关问题 C#组合框SelectedIndexChanged事件 - C# Combobox SelectedIndexChanged event C# combobox 上的 SelectedIndexChanged 方法存在问题 - C# problem with SelectedIndexChanged method on combobox C#Combobox SelectedIndexChanged没有更新datagridview - C# Combobox SelectedIndexChanged is not updating the datagridview 无法将组合框中的选定值分配给SelectedIndexChanged事件中的变量。 通过组合框选择C#Winforms填充文本框 - Cannot assign selected value from combobox to variable in SelectedIndexChanged event. Fill textboxes by combobox selection C# Winforms 在selectedindexchanged MVC C#后获取索引组合框 - get index combobox after selectedindexchanged mvc c# 在C#Windows窗体中为动态创建的组合框调用SelectedIndexChanged事件 - Calling SelectedIndexChanged Event for a dynamically created combobox in C# Windows Forms C#eBay SDK组合框将selectedindexchanged事件链接到按钮单击 - C# ebay sdk ComboBox linking selectedindexchanged event to button click 为什么comboBox1_SelectedIndexChanged的eventhandler永远不会在C#中执行 - why the eventhandler for a comboBox1_SelectedIndexChanged never gets executed in C# Fonts Combobox名单中的名字 XAML c# - Fonts names in the Combobox List XAML c# 更新组合框MVVM(XAML和C#) - Update combobox MVVM (XAML & C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM