简体   繁体   中英

Combo box items removal upon selection of a particular item

I have a combo box with 4 items. One of the items is "okay" which is a string. If this item is selected then I need to remove all other items in the combo box. How can I do this in XAML? Can I use control triggers?

You can do something like below.

In View:

<ComboBox ItemsSource="{Binding FilteredLenses}" SelectedItem="{Binding SelectedValue, Mode=TwoWay}" VerticalAlignment="Center" Width="230">
            </ComboBox>

In your ViewModel:

private ObservableCollection<string> filteredLenses;
public ObservableCollection<string> FilteredLenses
{
    get
    {
        if (filteredLenses == null)
            filteredLenses = new ObservableCollection<string>();

        return filteredLenses;
    }
    set
    {
        filteredLenses = value;
        OnPropertyChanged("FilteredLenses");
    }
}

public string selectedValue;
public string SelectedValue
{
    get
    {
        return selectedValue;
    }
    set
    {
        if (value == "Okay")
        {
            // This logic can be done in many ways
            FilteredLenses.Clear();
            FilteredLenses.Add("Okay");

            OnPropertyChanged("FilteredLenses");
        }

        selectedValue = value;
        OnPropertyChanged("SelectedValue");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM