I have two columns in my grid:
Name(Textbox) ---- ParentList(combobox).
A ----------------------- It sh
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.
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.
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;
}
}
}