【问题标题】:WPF ListBox Image Selected the saga continuesWPF ListBox Image Selected 传奇继续
【发布时间】:2010-09-28 03:26:31
【问题描述】:

好的,在我的 ListBox 中滚动带有文本的图像等。传奇还在继续。当我单击其中一个项目来选择它时,它会运行一个进程来打开 Web 浏览器并转到特定的 URL。我现在遇到的问题是,当 WPF 应用程序失去焦点并且 Web 浏览器打开时,在列表框中单击的项目变为白色。这是整个 ListBox XAML。我已将所选项目设置为透明,那么这是否与 WPF 应用失去焦点有关?

有什么我可以添加到运行进程的代码以打开 Web 浏览器以将焦点重新设置回 WPF 应用程序吗?

谢谢。

   <ListBox ItemsSource="{Binding Source={StaticResource WPFApparelCollection}}" Margin="61,-8,68,-18" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Hidden" SelectionMode="Single" x:Name="list1" MouseLeave="List1_MouseLeave" MouseMove="List1_MouseMove" Style="{DynamicResource ListBoxStyle1}" Background="Transparent" BorderThickness="0">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
                <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
                <Setter Property="Padding" Value="20,10,20,10" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border x:Name="Bd" SnapsToDevicePixels="true" Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter Property="Background" TargetName="Bd" Value="Transparent" />
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="true" />
                                        <Condition Property="Selector.IsSelectionActive" Value="false" />
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
                                </MultiTrigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal" IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <Grid>
                        <Image Source="{Binding Image}" MouseLeave="Image_MouseLeave" MouseEnter="Image_MouseEnter" Cursor="Hand" Tag="{Binding Link}" MouseLeftButtonDown="Image_MouseLeftButtonDown"  VerticalAlignment="Top" HorizontalAlignment="Left"></Image>
                    </Grid>
                    <Label Content="{Binding Name}" Cursor="Hand" Tag="{Binding Link}" MouseLeftButtonDown="Label_MouseLeftButtonDown" VerticalAlignment="Bottom" Foreground="White" Style="{StaticResource Gotham-Medium}" FontSize="8pt" HorizontalAlignment="Center" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【问题讨论】:

    标签: wpf image listbox focus selected


    【解决方案1】:

    我在 ListBox 中使用选择颜色时发现的一个技巧是使用系统画笔而不是与它们对抗。

    当一个 ListBox 获得焦点并选择了一个项目时,该项目的背景是 SystemColors.HighlightBrush。但是,当 ListBox 失去焦点时,所选项目的背景变为 SystemColors.ControlBrush。

    知道了这一点,您就可以覆盖该列表框的系统画笔,以便将其中的项目涂上您想要的颜色。

    <ListBox>
        <ListBox.Resources>
            <!-- override the system brushes so that selected items are transparent
                 whether the ListBox has focus or not -->
            <SolidColorBrush
                x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                Color="Transparent" />
            <SolidColorBrush
                x:Key="{x:Static SystemColors.ControlBrushKey}" 
                Color="Transparent" />
            <SolidColorBrush
                x:Key="{x:Static SystemColors.HighlightTextBrushKey}" 
                Color="Black" />
        </ListBox.Resources>
        <!-- ... your items here ... -->
    </ListBox>
    

    【讨论】:

    • 哇...你这家伙。非常好的主意。我得开始考虑 WPFish,而不是 Webform/Winform。再次感谢。约翰
    • 但是如果您的 ListBox 中有上下文菜单,则 ContextMenu 中的项目不会突出显示,对吗?我怎样才能实现这两种行为?
    • @jpsstavares 是的,从技术上讲,ContextMenus 与其所有者并不属于同一可视化树,因此不会继承画笔。您可能必须在 ContextMenu 的资源中复制它们,尽管我没有尝试过。
    • 我遇到了这种行为,所以我所做的是重新定义 ContextMenu 资源中的 HighlightBrushKey。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多