Karoline
Karoline

Reputation: 125

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

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

Upvotes: 0

Views: 612

Answers (2)

Devi Karthikeyan
Devi Karthikeyan

Reputation: 26

SelectionChanged property can be used for a combo box and a command can be bound to it. The corresponding action can be written in the Viewmodel against the command

Upvotes: 1

BradleyDotNET
BradleyDotNET

Reputation: 61379

The WPF ComboBox control (MSDN) does not have a SelectedIndexChanged event, you may be thinking of the WinForms one.

Beyond that; you should be using MVVM (MVVM: Tutorial from start to finish?) and not relying much (if at all) on control events. 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.

One final note; the use of Canvas there is almost certainly incorrect; you should just stick with the Grid so you have nice resizing instead of absolute positioning.

Upvotes: 1

Related Questions