简体   繁体   中英

Combobox assign null if value does not exist in itemsource

I have a Datagrid, and upon double clicking the selected row, an edit screen is launched. On this edit screen, there are combobox, whose value is binded to the selected row in the grid. Sometimes, the value assigned to the combobox does not exist in the combobox itemSource, so the display on the combobox is empty, but the value is not null. How can I update the value of the selected item, to be null if the value does not exist in the itemsource collection.

In the above scenario, Since, the second screen is bound to the Selected item on the first sreen, the SelectedValue for City is "Los Angeles" and the Display is Empty. But since "Los Angeles" does not exist in the collection, SelectedValue should be null.

A solution is to set the ItemsSource of the combobox to the list (example: "DeviceNameList") and set the SelectedItem of this combobox to a variable that matches the type of elements inside your list (SelectedDeviceName).

Now when you load your edit screen, it will have bound the list to the combobox and display the variable you set.

You have to write some code to check if the selected item occurs in the list and if not you can set the value to zero.

Example:

XAML code:

<ComboBox ItemsSource="{Binding Path=DeviceNameList}" SelectedItem="{Binding Path=SelectedDeviceName}" />

Code to set the selectedItem:

    /// <summary>
    /// Gets or sets SelectedDeviceName.
    /// </summary>
    public ObservableCollection<string> DeviceNameList
    {
         get
        {
           return mDeviceNameList;
        }

        set
        {
            mDeviceNameList = value;
        }
    }

    /// <summary>
    /// Gets or sets SelectedDeviceName.
    /// </summary>
    public string SelectedDeviceName
    {
        get
        {
            return mSelectedDeviceName;
        }

        set
        {
            mSelectedDeviceName = value;
            NotifyPropertyChanged("SelectedDeviceName");
        }
    }

    /// <summary>
    /// Event PropertyChanged
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;


        /// <summary>
    /// Function NotifyPropertyChanged
    /// </summary>
    /// <param name="property">
    /// The property.
    /// </param>
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

Converters are used to translate the data in the binding to the desired result. (Allowing incompatible types to be matched between source and target).

A similar question on converter used between a combobox and itemsource is in here:

Use converter on bound items in combobox

and you can read more aboud converters in general here:

http://wpftutorial.net/ValueConverters.html

In this case, the converter should be placed on selectedItem property of the combo.

EDIT:

I've played a bit with a sample of code, and registeed to the selection changed event in the combo box. this event is only triggered if the source for the selection item yields a selection which is valid for the combo box. So now I return to my original suggestion which should work: In the converter , check the incoming value against the list of all values of the combobox items. If it does not exist , the converter needs to let the combo know it should set its selected item to null. While I'm sure there are more simple and elegant ways to do this (I'm not a WPF expert) this should do the trick.

Another lead on how to hook the converter with the control is using multibinding:

Can a WPF converter access the control to which it is bound?

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