Haider Khattak
Haider Khattak

Reputation: 577

Hide/Unhide Controls using combobox in WPF windows

i want to hide and unhide some labels and a textbox using combobox. The textbox is populated from database using datasource. I have tried the code but the application is not giving any response.

Code:

private void _cmbRole_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_cmbRole.SelectedValue == "3")
            {
                _txtPmdc.Visibility = Visibility.Visible;
                _lblAffiliation.Visibility = Visibility.Visible;
                _lblPmdb.Visibility = Visibility.Visible;
            }

            else
            {
                _txtPmdc.Visibility = Visibility.Hidden;
                _lblAffiliation.Visibility = Visibility.Hidden;
                _lblPmdb.Visibility = Visibility.Hidden;
            }
        }

xaml:

<ComboBox x:Name="_cmbRole" Grid.Column="2" DisplayMemberPath="type" ItemsSource="{Binding}" Margin="10,14,52,10" Grid.Row="1" SelectedValuePath="role_id" FontSize="14" SelectionChanged="_cmbRole_SelectionChanged">
                        <ComboBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel/>
                            </ItemsPanelTemplate>
                        </ComboBox.ItemsPanel>
                    </ComboBox>

Upvotes: 0

Views: 146

Answers (2)

Koopakiller
Koopakiller

Reputation: 2883

I think SelectedValue is not a string, so the comparison is always false. Try the following:

if (_cmbRole.SelectedValue.ToString() == "3")

The better way is to compare the SelectedValue with a value of the correct type. For example if it is an integer, you should compare it with 3.

But you can also compare the SelectedIndex and SelectedItem property. Possibly is that the better choice.

Upvotes: 1

Praveen Paulose
Praveen Paulose

Reputation: 5771

Looks like you are comparing the wrong data type. You are binding to Role ID which in most probability is an integer and you are comparing it with a string when you write == "3"

If it is an integer, you should be writing

if (_cmbRole.SelectedValue == 3)
{
    //true block here
}

Upvotes: 0

Related Questions