在MouseOver上的ItemTemplate中的WPF列表框显示按钮

时间:2008-10-15 21:09:15

标签: wpf listbox

我有一个包含图片和按钮的列表框。默认情况下,该按钮是隐藏的。每当我将鼠标悬停在列表框中的某个项目上时,我想让按钮可见。 我正在使用的XAML如下。感谢

<Window.Resources>
        <Style TargetType="{x:Type ListBox}">
    <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Border BorderBrush="Black" BorderThickness="1" Margin="6">
                                <StackPanel Orientation="Horizontal">
                                    <Image Source="{Binding Path=FullPath}" Height="150" Width="150"/>
                                    <Button x:Name="sideButton" Width="20" Visibility="Hidden"/>
                                </StackPanel>
                            </Border>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
        </Style>
    </Window.Resources>

5 个答案:

答案 0 :(得分:41)

好的,请在按钮声明中尝试:

<Button x:Name="sideButton" Width="20">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Visibility" Value="Hidden" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsMouseOver}" Value="True">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

所以我使用带触发器的样式来查找可视树,直到找到ListBoxItem,当IsMouseOver属性翻转到True时,我设置了buttonVisible的可见性。

看看它是否接近你想要的东西。

答案 1 :(得分:14)

Style可以满足您的需求。鼠标悬停时,只有当指针位于ListBoxItem上方时,该按钮才会显示。特殊技巧是绑定到TemplatedParent以达到IsMouseOver并使用TargetName上的Setter仅影响Button

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border BorderBrush="Black"
                        BorderThickness="1"
                        Margin="6">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding Path=FullPath}"
                               Height="150"
                               Width="150" />
                        <Button x:Name="sideButton"
                                Width="20"
                                Visibility="Hidden" />
                    </StackPanel>
                </Border>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding IsMouseOver,RelativeSource={RelativeSource TemplatedParent}}"
                                 Value="True">
                        <Setter Property="Visibility"
                                TargetName="sideButton"
                                Value="Visible" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

答案 2 :(得分:5)

@David正在展示正确的方式, 但我对您的XAML架构有一个建议。如果Button上没有任何DataBinding,最好将它放在ListBoxItem样式中,而不是像DataTemplate那样。

  <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border BorderBrush="Black"
                    BorderThickness="1"
                    Margin="6">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding Path=FullPath}"
                           Height="150"
                           Width="150" />                             
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>            
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid Background="Transparent">
                        <Button x:Name="sideButton" Width="20" HorizontalAlignment="Right" Visibility="Hidden" />
                        <ContentPresenter/>                            
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger  Property="IsMouseOver" Value="True">
                            <Setter Property="Visibility"
                            TargetName="sideButton"
                            Value="Visible" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>            
    </Style>

答案 3 :(得分:0)

查找单击项目的一个解决方案是添加以下事件设置器

XAML             

C# void ListBoxItem_MouseEnter(对象发送者,MouseEventArgs e)         {             _memberVar =(发送者为ListBoxItem).Content;         }

答案 4 :(得分:0)

  

只是想知道,如果我们使用上述技术,我们如何确定按钮被点击的项目?

要回答Brian的问题,在按钮点击处理程序中,您可以沿着可视树向上查找包含该按钮的项目:

        DependencyObject dep = (DependencyObject)e.OriginalSource;
        while ((dep != null) && !(dep is ListBoxItem))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }

        if (dep != null)
        {
            // TODO: do stuff with the item here.
        }