WPF列表框图像选择传奇继续 [英] WPF ListBox Image Selected the saga continues

查看:121
本文介绍了WPF列表框图像选择传奇继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,在我的列表滚动图像瓦特/文字等传奇继续。当我点击其中一个项目,选择它,运行一个进程打开一个Web浏览器,并转到一个特定的URL。我现在遇到的问题是,当WPF应用程序失去焦点,Web浏览器打开时,列表框中单击的项目变成白色。这是整个列表框XAML。我已经设置了选定的项目是透明的,所以这与WPF应用程序失去焦点有关吗?

有什么我可以添加汤姆运行的代码过程打开Web浏览器设置焦点回WPF应用程序?

谢谢。

 < ListBox ItemsSource ={Binding Source = {StaticResource WPFApparelCollection}}Margin =61,-8,68,-18ScrollViewer.VerticalScrollBarVisibility =HiddenScrollViewer.Horizo​​ntalScrollBarVisibility =HiddenSelectionMode =Singlex:Name =list1MouseLeave =List1_MouseLeaveMouseMove =List1_MouseMoveStyle ={DynamicResource ListBoxStyle1}Background =TransparentBorderThickness =0> 
< ListBox.ItemContainerStyle>
< Style TargetType ={x:Type ListBoxItem}>
< Setter Property =BackgroundValue =Transparent/>
< Setter Property =Horizo​​ntalContentAlignmentValue ={Binding Path = Horizo​​ntalContentAlignment,RelativeSource = {RelativeSource AncestorType = {x:Type ItemsControl}}}/>
< Setter Property =VerticalContentAlignmentValue ={Binding Path = VerticalContentAlignment,RelativeSource = {RelativeSource AncestorType = {x:Type ItemsControl}}}/>
< Setter Property =PaddingValue =20,10,20,10/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type ListBoxItem}>
< Border x:Name =BdSnapsToDevicePixels =trueBackground =TransparentBorderBrush ={TemplateBinding BorderBrush}BorderThickness ={TemplateBinding BorderThickness}Padding ={TemplateBinding Padding}>
< ContentPresenter Horizo​​ntalAlignment ={TemplateBinding Horizo​​ntalContentAlignment}VerticalAlignment ={TemplateBinding VerticalContentAlignment}SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}/>
< / Border>
< ControlTemplate.Triggers>
<触发属性=IsSelectedValue =true>
< Setter Property =BackgroundTargetName =BdValue =Transparent/>
< Setter Property =ForegroundValue ={DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}/>
< / Trigger>
< MultiTrigger>
< MultiTrigger.Conditions>
< Condition Property =IsSelectedValue =true/>
< Condition Property =Selector.IsSelectionActiveValue =false/>
< Setter Property =BackgroundTargetName =BdValue ={DynamicResource {x:Static SystemColors.ControlBrushKey}}/>
< Setter Property =ForegroundValue ={DynamicResource {x:Static SystemColors.ControlTextBrushKey}}/>
< / MultiTrigger>
<触发属性=IsEnabled值=假>
< Setter Property =ForegroundValue ={DynamicResource {x:Static SystemColors.GrayTextBrushKey}}/>
< / Trigger>
< / ControlTemplate>
< / Setter>
< / style>
< /ListBox.ItemContainerStyle>
< ListBox.ItemsPanel>
< ItemsPanelTemplate>
< VirtualizingStackPanel Orientation =Horizo​​ntalIsItemsHost =True/>
< / ItemsPanelTemplate>
< /ListBox.ItemsPanel>
< ListBox.ItemTemplate>
< DataTemplate>
< StackPanel Orientation =Vertical>
<网格> MouseLeave =Image_MouseLeaveMouseEnter =Image_MouseEnterCursor =HandTag ={Binding Link}MouseLeftButtonDown =Image_MouseLeftButtonDownVerticalAlignment =TopHorizo​​ntalAlignment ={ 左 >< /图像>
< / Grid>


解决方案

在一个列表框中是使用系统画笔而不是与他们作战。

当一个ListBox被选中并且一个项目被选中时,该项目的背景是SystemColors.HighlightBrush。当ListBox失去焦点时,所选项目的背景就变成了SystemColors.ControlBrush。



知道这个,你可以覆盖这个ListBox的系统刷子 >

 < ListBox> 
< ListBox.Resources>
<! - 覆盖系统画笔,以便所选项目是透明的
列表框是否具有焦点 - >
< 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>
<! - - 您的物品在这里... - >
< / ListBox>


Ok in my ListBox scrolling images w/ text, etc. the saga continues. When I click one of the items, to select it, that runs a process to open a web browser and go to a specific URL. The problem I'm having now is that when the WPF app loses focus, and the web browser opens, the item clicked inside the listbox turns white. Here's the whole ListBox XAML. I have set the selected items to transparent, so does this have something to do with the WPF app losing focus?

Is there something I can add tom the code that runs the process to open the web browser to set focus back to the WPF app?

Thanks.

   <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>

解决方案

One trick I've found when playing with selection colours in a ListBox is to work with the system brushes rather than fighting against them.

When a ListBox is focused and an item is selected, that item's background is SystemColors.HighlightBrush. When the ListBox loses focus, however, the selected item's background becomes SystemColors.ControlBrush.

Knowing this, you can override the system brushes for that ListBox so that the items within are painted with the colours you want.

<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>

这篇关于WPF列表框图像选择传奇继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆