根据列表框ListboxItem选择的按钮可见性

时间:2014-12-22 08:33:43

标签: c# wpf listbox show-hide

我的情况如下

我想在没有选择项目时隐藏删除按钮&显示什么时候被选中。

如果用户选择了一个项目,也会隐藏该按钮。点击其他地方(表示用户不再使用列表框)。

我尝试LostFocus个事件,检查SelectedIndex等但没有成功。知道怎么做吗?

    private void ListBoxItem_LostFocus(object sender, RoutedEventArgs e)
    {
        if (button.IsFocused != true) // checking if user has selected an item & clicking on button (valid action)
        {
            listbox.SelectedIndex = -1;
        }
    }

    private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (listbox.SelectedIndex == -1)
        {
            button.Visibility = Visibility.Hidden;
        }
        else
        {
            button.Visibility = Visibility.Visible;
        }
    }

4 个答案:

答案 0 :(得分:1)

约瑟夫的回答是正确的,但你不需要转换器。

您可以通过简单的触发器获得相同的结果:

<ListBox>
    ...
    <ListBox.Style>
        <Style TargetType="{x:Type ListBox}">
            <Style.Triggers>
                <Trigger Property="SelectedItem" Value="{x:Null}">
                    <Setter TargetName="YOUR_BUTTON'S_NAME" Property="Visibility" Value="Hidden"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>
</ListBox>

编辑:在Style Setter中使用TargetName确实是不可能的。

所以,我能做的最接近的解决方案是创建一个DataTrigger:

<DockPanel LastChildFill="True">
    <Button DockPanel.Dock="Top">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=MyList, Path=SelectedItem}"
                                 Value="{x:Null}">
                        <Setter Property="Visibility" Value="Hidden"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
        <TextBlock Text="Button"/>
    </Button>
    <ListBox x:Name="MyList">
        <TextBlock Text="Something"/>
        <TextBlock Text="Something"/>
        <TextBlock Text="Something"/>
        <TextBlock Text="Something"/>
        <TextBlock Text="Something"/>
    </ListBox>
</DockPanel>

答案 1 :(得分:0)

对于问题的前半部分,最好的方法是使用这样的转换器: 1.添加转换器类:

 public class VisibilityConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value == null ? Visibility.Hidden : Visibility.Visible);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

2.将其添加到Window资源:

<Window.Resources>
        <wpfApplication:VisibilityConverter x:Key="VisibilityConverter"/>
</Window.Resources>

3。并用它来隐藏/显示你的按钮:

<Grid>
       <Grid.RowDefinitions>
           <RowDefinition Height="Auto"/>
           <RowDefinition Height="Auto"/>
       </Grid.RowDefinitions>
        <ListBox HorizontalAlignment="Center" x:Name="listBox">
            <ListBoxItem Content="Fist"/>
            <ListBoxItem Content="Second"/>
            <ListBoxItem Content="Third"/>
            <ListBoxItem Content="Fourth"/>
        </ListBox>
        <Button Content="delete" Grid.Row="1" HorizontalAlignment="Center" Visibility="{Binding SelectedItem,ElementName=listBox,Converter={StaticResource VisibilityConverter}}"/>
    </Grid>

对于问题的后半部分,您必须在窗口中处理MouseDown事件:

MouseDown="MainWindow_OnMouseDown"

并添加以下处理程序(将列表的SelectedItem重置为null

 private void MainWindow_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            listBox.SelectedItem = null;
        }

答案 2 :(得分:0)

单独运行此代码,try.it在这里为我工作。

  <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <StackPanel>
        <ListBox x:Name="lst">
            <ListBox.Triggers>
                <EventTrigger RoutedEvent="GotFocus">
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DeleteButton" Storyboard.TargetProperty="Visibility">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static Visibility.Visible}"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard> 
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="LostFocus">
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DeleteButton" Storyboard.TargetProperty="Visibility">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static Visibility.Collapsed}"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </ListBox.Triggers>
            <ListBoxItem>ListBoxItem1</ListBoxItem>
            <ListBoxItem>ListBoxItem2</ListBoxItem>
            <ListBoxItem>ListBoxItem3</ListBoxItem>
            <ListBoxItem>ListBoxItem4</ListBoxItem>
        </ListBox>
        <Button x:Name="DeleteButton"  Content="Delete" Height="30" Width="100">
            <Button.Style>
                <Style BasedOn="{StaticResource alredayDefinedStyleKey}"  TargetType="Button">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                    <Style.Triggers>                           
                        <DataTrigger Binding="{Binding Path=SelectedItem, ElementName=lst}" Value="{x:Null}" >
                            <Setter Property="Visibility" Value="Collapsed"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
    </StackPanel>

    <ListBox Grid.Column="1">
        <ListBoxItem>1</ListBoxItem>
        <ListBoxItem>2</ListBoxItem>
    </ListBox>
</Grid>

答案 3 :(得分:-1)

如果元素失去逻辑焦点,

LostFocus将仅触发。使用LostKeyboardFocus事件。那你就近了一步。