none
Collapse/Remove 1 control within a ListBox based on condition RRS feed

  • Question

  • Hi,

    I am trying out a use case where a listbox has label and a button. The button should be visible only for the new item until the progressBar is visibile for example. And after that the listbox should have only label.

    I tried setting trigger for button to control the visibility, however the trigger is applied to all the items in the list, however i want the button to be visible for the new item that is added.

    Any suggestion is appreciated.

    Please find below what i tried:

    <Style x:Key="ButtonVisibilityStyle" TargetType="{x:Type Button}">
                    <Style.Triggers>                    
                       <DataTrigger Binding="{Binding ElementName=ProgressBar, Path=Visibility}" Value="Hidden">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
    <ListBox Name="SeriesListBox" ItemsSource="{Binding List, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <Label Name="Label" Content="{Binding Text}"/> 
    <Button Name="CanceleButton" Style="{StaticResource ButtonVisibilityStyle}"/> 
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
    Thanks in advance.


    • Edited by Grace SC Wednesday, June 19, 2019 5:34 AM
    Thursday, June 13, 2019 8:43 AM

Answers

  • The problem is you only have one source (ProgressBar) to set the trigger.  To make it work you would need to have a property in whatever object ResultSeriesList contains that sets the visibility.

    Lloyd Sheen

    • Marked as answer by Grace SC Wednesday, June 19, 2019 5:32 AM
    Thursday, June 13, 2019 12:35 PM

All replies

  • The problem is you only have one source (ProgressBar) to set the trigger.  To make it work you would need to have a property in whatever object ResultSeriesList contains that sets the visibility.

    Lloyd Sheen

    • Marked as answer by Grace SC Wednesday, June 19, 2019 5:32 AM
    Thursday, June 13, 2019 12:35 PM
  • For others looking for a similar issue, Please find below what i tried:

    <Style x:Key="ButtonVisibilityStyle" TargetType="{x:Type Button}">
                    <Style.Triggers> 

             <Setter Property="Visibility" Value="Collapsed"/>

                      <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=ProgressBar, Path=Visibility}" Value="Visible"/>
                                <Condition Binding="{Binding IsNewItem}" Value="True"/>
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter Property="Visibility" Value="Visible"/>
                            </MultiDataTrigger.Setters>       
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
    <ListBox Name="SeriesListBox" ItemsSource="{Binding List, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
    <Label Name="Label" Content="{Binding Text}"/> 
    <Button Name="CanceleButton" Style="{StaticResource ButtonVisibilityStyle}"/> 
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

                                                 
    Wednesday, June 19, 2019 5:36 AM