I have a ListBox control populated with several ListBox items. Each item contains a "Proceed" button and a "Postpone" button. I would like to hide that ListBox item (presented as a row in my case) once the "Postpone" button is clicked. The code I have currently doesn't seem to have any effect.


         <Style TargetType="{x:Type ListBoxItem}">
                   <DataTrigger Binding="{Binding PostponeClicked}" Value="1">


 private void PostponeThirdPartyUpdatesButton_Click(object sender, RoutedEventArgs e)
        DataTrigger d = new DataTrigger();
        d.Binding = new Binding("PostponeClicked");
        d.Value = 1;

        var context = ((FrameworkElement)sender).DataContext as Tuple<RegScan_ThirdParty.InstalledApplicationFromRegistryScan, RegScan_ThirdParty.ManifestRequiredApplication, RegScan_ThirdParty.RequiredApplicationState>;

        Button ThirdPartyPostponeButton = sender as Button;
        ThirdPartyPostponeButton.IsEnabled = false;

        if (context != null)

        ThirdPartyPostponeButton.IsEnabled = true;

  • I'll take either. – user3342256 Mar 20 '14 at 5:44
  • Do you have a viewmodel for every item in the listbox (MVVM style)? In that viewmodel you could handle the button click via command and set a property (e.g. Visibility) to Visibility.Collapsed and bind it to your listboxitem. – Mighty Badaboom Mar 20 '14 at 7:38
  • I was going to say the same thing as Nergal... So I wrote it up as an answer. – NielW Mar 20 '14 at 19:28

I had to address the same thing once. Each item in your list box should be an object. We'll call it MyObject for now, since I have no idea what your object type is. In the MyObject class, you'll put your Proceed and Postpone commands.

//ViewModelBase implements INotifyPropertyChanged, which allows us to call RaisePropertyChanged, and have the UI update
class MyObject : ViewModelBase
    private bool isNotPostponed = true;
    public bool IsNotPostponed
        get { return isNotPostponed; }
            isNotPostponed = value; 

    private Command postponeCommand;
    public Command PostponeCommand

            if (postponeCommand == null)
                postponeCommand = new Command(PostponeCommand);
            return postponeCommand; 


    private void Postpone(object x)
        IsNotPostponed = false;

    //similar code for Proceed Command

Then in the viewmodel of the view that displays the listBox, create a List that you can bind to your listbox (or whatever collection you want to use). I called it MyObjectsList in the XAML below. (I'm not showing the ViewModel code where this object lives, but I assume you have code for binding to the ListBox.) Then in your ItemsControl.ItemTemplate, bind to each MyObject in your List.

<ItemsControl ItemsSource="{Binding MyObjectsList}">
                <BooleanToVisibilityConverter x:Key="boolToVis"/>
            <StackPanel Visibility="{Binding IsNotPostponed, Converter={StaticResource boolToVis}}">
                <Button Command="{Binding PostponeCommand}" Content="Postpone"/>
                <Button Command="{Binding ProceedCommand}" Content="Proceed"/>

When Postpone is clicked, the command will execute Postpone(), which will set IsNotPostponed to false. On setting IsNotPostponed to false, RaisePropertyChanged tells the UI that IsNotPostponed changed (you need to implement the INotifyPropertyChanged interface.) Lastly, when the UI gets the change notification, it converts the bool to a Visibility. True => Visible, False => Collapsed.

  • Sorry, I didn't use a ListBox in my code, even though I mentioned listboxes... An ItemsControl is just generic ListBox. This code should work fine. – NielW Mar 20 '14 at 19:30
  • IsNotPostponed is a terrible name, but the boolean needs to match True==Visible, False==Collapsed... Call it what you want. – NielW Mar 20 '14 at 19:44

Your Answer

