WPF : How to bind Button's visibility inside ListBox ItemsControl using selecteditem information ?

3.1k views Asked by At

I have a list box binds to a ObservableCollection of Items. Each listboxItem contains a few textblocks and a few buttons. I would like to show the buttons inside the listboxItem only when the listbox item is selected or hightlight. If the listbox item is not highlighted, the button should be hidden. I tried to use datatrigger to bind to the IsSelected property. However it does not work. Please advice. thanks

        <ListBox Name="LayoutListBox" SelectedItem="{Binding Path=SelectedLayout, Mode=TwoWay}" ItemsSource="{Binding Layouts}" SelectionMode="Single" HorizontalContentAlignment="Stretch">
      <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
          <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Gray"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Gray"/>
          </Style.Resources>
        </Style>
      </ListBox.ItemContainerStyle>
      <ListBox.ItemTemplate>
        <DataTemplate>
          <Grid>
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Border HorizontalAlignment="Stretch" Margin="2" CornerRadius="2" BorderBrush="#80808080" BorderThickness="1" Background="GhostWhite">
                    <Grid>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height ="*"/>
                                    <RowDefinition Height ="*"/>
                                </Grid.RowDefinitions>
                                <DockPanel Grid.Row="1" Grid.Column="3" DockPanel.Dock="Right">
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Visibility="{Binding Path=ViewName, Converter={StaticResource Converter}, ConverterParameter=false}">
                                        <Button ToolTip="Save">
                                            <Image Source="/Common.View;component/LayoutManager/View/Images/PushPin.png" />
                                        </Button>
                                        <Button Click="EditButtonClick" ToolTip="Edit">
                                            <Image Source="/Common.View;component/LayoutManager/View/Images/pencil.png" />
                                        </Button>
                                        <Button Click="DeleteButtonClick" ToolTip="Delete">
                                            <Image Source="/Common.View;component/LayoutManager/View/Images/cross.png" />
                                        </Button>
                                    </StackPanel>
                                </DockPanel>
                            </Grid>
                        </DockPanel>
                    </Grid>
                  </Border>
          </Grid>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
2

There are 2 answers

0
ReeganLourduraj On BEST ANSWER

I attached a simple example to show button in listbox item when you selected. it should have to bind as RelativeSource and have to use Converter

Example:

<Window.Resources>
        <BooleanToVisibilityConverter x:Key="booleanVisibleConverter" />
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding ObservableCollection}" Grid.Row="0" SelectedItem="{Binding Path=Item}" 
                 SelectionMode="Single" IsSynchronizedWithCurrentItem="True" Margin="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectedIndex="0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="{Binding}" />
                        <Button Content=" X " Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected, Converter={StaticResource booleanVisibleConverter}}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
0
Liz On

You can create a style to associate with each of your buttons as follows:

<Style TargetType="{x:Type Button}">
  <Setter Property="Visibility" Value="Collapsed"/>
    <Style.Triggers>
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource 
                            Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},
                            Path=IsSelected}" Value="True">
           <Setter Property="Visibility" Value="Visible"/>
     </DataTrigger>
  </Style.Triggers>
</Style>