0

I have two columns in my grid:

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

A ----------------------- It should comprise of only B and C

B ----------------------- A and C

C ----------------------- A and B

My ParentList contains {A,B,C}.

How can I hide the particular item from the list depending on the text in the textbox?

XAML for my code is:

<Grid>
    <StackPanel >          
        <ListView>
            <ListView.View>  
                <GridView >
                    <GridViewColumn  Header="Name"  >
                        <GridViewColumn.CellTemplate >
                            <DataTemplate >
                                <WrapPanel >
                                    <TextBox x:Name="txName"  Text="{Binding Name}"  />                                      
                                </WrapPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn  Header="Parent List"  >
                        <GridViewColumn.CellTemplate >
                            <DataTemplate >
                                <WrapPanel >
                                    <ComboBox x:Name="cbParentId" ItemsSource="{Binding Path=ParentList,ElementName=UI}"   />
                                </WrapPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </StackPanel>
</Grid>

3 Answers 3

0

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.

5
  • -Or- use a CollectionView.
    – Fede
    Dec 5, 2013 at 14:24
  • @HighCore: Can you give an example of CollectionView?
    – zooney
    Dec 5, 2013 at 14:39
  • @Zooney, whenever you want to find something out about WPF, just go to a search engine and type 'WPF objectName Class' then click on the top answer which will almost certainly be for MSDN. That's what I just did: CollectionView Class... it'll save you loads of time.
    – Sheridan
    Dec 5, 2013 at 14:53
  • @Sheridan :Is there a way in wpf itself other than changing the data structure?Thx..
    – zooney
    Dec 5, 2013 at 15:16
  • There is nothing 'built in' to WPF that will do this... my example was not 'built in' to WPF. There are other ways of achieving this, but this is the only one that I'd recommend. For instance, you could attach a ComboBox.SelectionChanged handler and filter your collection from there, but the solution above is by far the simplest.
    – Sheridan
    Dec 5, 2013 at 15:23
0

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;
            }
        }
    }
-1

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.

4
  • Can u give an example?
    – zooney
    Dec 5, 2013 at 14:23
  • @zooney For the MultiBinding or for the Converter?
    – Programmer
    Dec 5, 2013 at 14:25
  • -1 Not only you're not providing any code, your solution is also bad.
    – Fede
    Dec 5, 2013 at 14:25
  • @user2760623 : for both.
    – zooney
    Dec 5, 2013 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.