Change combo box item list depending on the Textbox Text

后端 未结 3 860
广开言路
广开言路 2021-01-27 16:29

I have two columns in my grid:

Name(Textbox) ---- ParentList(combobox).

A ----------------------- It sh

相关标签:
3条回答
  • 2021-01-27 16:54

    You can make the ItemsSource as MultiBinding to be binded to the list and the textbox, and in the converter go over the list, and hide items you don't want (according to the textbox).

    Not a too nice solution, but it should work.

    This is the multibinding:

    <MultiBinding Converter="{StaticResource myConverter}">
      <Binding ElementName="UI" Path="ParentList" />
      <Binding ElementName="txName" Path="Text" />
    </MultiBinding>
    

    And in the convert method run with foreach on the parnetlist, and conditions that if the ListItem equals the text, it should be collapsed.

    0 讨论(0)
  • 2021-01-27 17:15

    That's easy to achieve if you slightly adapt your data type class... I've had to guess at the type of your ParentList because you didn't show it:

    private List<string> allItems = GetAllItems();
    
    public ObservableCollection<string> ParentList
    {
        get { return parentList; }
        set
        {
            parentList = value;
            NotifyPropertyChanged("ParentList");
        }
    }
    
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            NotifyPropertyChanged("Name");
            ParentList = new ObservableCollection<string>(allItems.Where(i => IsFiltered(i)));
        }
    }
    
    private bool IsFiltered(string item)
    {
        // implement your filter condition here
        return item.StartsWith("A");
    }
    

    So the basic idea is that you hold a private collection of all the possible values... this remains unchanged. Each time the Name property is changed, we create a new ParentList dependant on some filtering condition in the IsFiltered method.

    0 讨论(0)
  • 2021-01-27 17:15

    The simple and best solution [for me] would be to write a 'GotFocus' event and apply the Visibility on the required item.

        private void combobox_GotFocus_1(object sender, RoutedEventArgs e)
        {          
            var combobox = sender as ComboBox;
    
            if (combobox == null) return;
            var model = combobox.DataContext as Model;
    
            foreach (var item in combobox.ItemsSource)
            {
                if (item.Equals(model.Name))
                {
                    var comboboxItem = combobox.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem;
                    if (comboboxItem != null)
                        comboboxItem.Visibility = Visibility.Collapsed;
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题