Combobox selectedindex = -1还删除可编辑组合框中写入的文本

时间:2018-04-28 17:25:24

标签: wpf mvvm combobox

问题是当我使selectedindex = -1时,它会删除组合框中写入的文本。我只想取消选择下拉列表中的项目,但文本应保留在comboxbox文本区域中。

我也尝试使用selecteditem = null但它也有相同的行为。我在下面添加我的项目。

我正在使用输入验证并输入键命令,因此在按Enter键后会考虑所有结果。

如果需要更多详细信息,请告诉我。

视图模型

public class ViewModel : ValidateObservableObject
{
    public ViewModel()
    {
        this.ErrorsChanged += RaiseCanExecuteChanged;
    }

    private void RaiseCanExecuteChanged(object sender, DataErrorsChangedEventArgs e)
    {
        if (this.HasErrors)
        {
            ItemSelected = -1;  //It also deletes the text written in the combobox
        }

        else
        {
            Locations.Add(Location);
            SelectedLocation = Location;

        }
    }

    private int _itemSelected;

    public int ItemSelected
    {
        get => _itemSelected;
        set
        {
            _itemSelected = value;
            OnPropertyChanged("ItemSelected");
        }
    }

    private RelayCommand _comboBoxEnterKeyCommand;

    public RelayCommand ComboxBoxEnterKeyCommand => _comboBoxEnterKeyCommand ?? (_comboBoxEnterKeyCommand = new RelayCommand(EnterPress));

    private void EnterPress(object obj)
    {
        Location = (string) obj;


    }

    private ObservableCollection<string> _locations;

    public ObservableCollection<string> Locations
    {
        get { return _locations ?? (_locations = new ObservableCollection<string>()); }
        set
        {
            _locations = value;
            OnPropertyChanged(nameof(Locations));
        }
    }


    private string _location;

    [RegularExpression("^[a-zA-Z0-9 ]*$", ErrorMessage = "Special characters not allowed")]
    public string Location
    {
        get { return _location; }
        set
        {
            SetProperty(ref _location, value);
        }
    }

    private string _selectedLocation;

    public string SelectedLocation
    {
        get { return _selectedLocation; }
        set
        {
            _selectedLocation = value;
            OnPropertyChanged(nameof(SelectedLocation));
        }
    }
}

查看

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <ComboBox Margin="10" x:Name="Combobox" IsEditable="True" SelectedIndex="{Binding Path = ItemSelected}" ItemsSource="{Binding Locations}">
        <ComboBox.InputBindings>
            <KeyBinding Command="{Binding ComboxBoxEnterKeyCommand}" CommandParameter="{Binding ElementName=Combobox,Path=Text}" Key="Enter"/>
        </ComboBox.InputBindings>
        <ComboBox.Text>
            <Binding Path="Location" UpdateSourceTrigger="LostFocus" ValidatesOnNotifyDataErrors="True" Mode="TwoWay" />
        </ComboBox.Text>
        <ComboBox.VerticalContentAlignment>Center</ComboBox.VerticalContentAlignment>
        <ComboBox.IsTextSearchEnabled>False</ComboBox.IsTextSearchEnabled>
        <ComboBox.SelectedItem>
            <Binding Path="SelectedLocation" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"  />
        </ComboBox.SelectedItem>
    </ComboBox>
</Grid>

0 个答案:

没有答案