【问题标题】:Listbox Item selection列表框项目选择
【发布时间】:2017-09-07 14:48:00
【问题描述】:

我有一个 WPF 项目(C#、Visual Studio 2010、MVVM)。

在这个项目中,我有一个 ListBox。当我选择项目时,我的对象周围出现了一个蓝色框,但我不确定该框来自何处。显然这与选择有关,但我不确定要改变什么才能让它消失。

列表框在这里:

<ListBox ItemsSource="{Binding ChatNodeListViewModel.ChatNodeVMs, Source={StaticResource Locator}}" Background="Transparent" Name="LbNodes" SelectedItem="{Binding ChatNodeListViewModel.SelectedNode, Source={StaticResource Locator}}" >
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas HorizontalAlignment="Left" VerticalAlignment="Top" Width="2000" Height="1600"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Canvas.Left" Value="{Binding XCoord}"/>
                    <Setter Property="Canvas.Top" Value="{Binding YCoord}"/>
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="lb_PreviewMouseLeftButtonDown" />

                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Thumb Name="myThumb" Template="{StaticResource NodeVisualTemplate}">

                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="DragDelta">
                                    <cmd:EventToCommand Command="{Binding ChatNodeListViewModel.DragDeltaCommand, Source={StaticResource Locator}}" PassEventArgsToCommand="True"/>
                                </i:EventTrigger>

                            </i:Interaction.Triggers>
                        </Thumb>
                    </Grid>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding NodeVisualMode}" Value="0">
                            <Setter TargetName="myThumb" Property="Template" Value="{StaticResource NodeVisualTemplate}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding NodeVisualMode}" Value="1">
                            <Setter TargetName="myThumb" Property="Template" Value="{StaticResource NodeTemplateTest}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding NodeVisualMode}" Value="2">
                            <Setter TargetName="myThumb" Property="Template" Value="{StaticResource NodeTemplateTest2}" />
                        </DataTrigger>

                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

有一个主要的 ControlTemplate 可以为其他人切换(主要的是 NodeVisualTemplate)。但由于这个问题似乎发生在所有人身上,我倾向于相信它更进一步。

不过,我确实对其进行了一些测试,并将在此处展示:

<ControlTemplate x:Key="NodeVisualTemplate">
            <Grid>
                <Border x:Name="_Border" Padding="1" SnapsToDevicePixels="true" BorderThickness="3" Margin="2" CornerRadius="5,5,5,5" BorderBrush="SteelBlue"                     Visibility="Visible">

                </Border>

            </Grid>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="true">
                    <Setter TargetName="_Border" Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect ShadowDepth="0" Color="Black" Opacity="1" BlurRadius="20" />
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="_Border" Property="Background" Value="Transparent"/>
                </DataTrigger>

            </ControlTemplate.Triggers>
        </ControlTemplate>

此模板顶部的边框内有一些文本框和一些东西,但我不怀疑它们是其中的一部分。关键是我改变了这个控件模板的背景。它在上面的 XAML 中是“透明的”,但我已将其更改为红色和其他颜色以进行测试。它并没有摆脱 ListBoxItem 周围的蓝色方块。

这是我得到的可怕蓝色背景的图像:

我可以摆脱它吗?老实说,我真的很茫然。

谢谢。

编辑:我应该提一下我尝试过的其他一些在这里相当重要的事情。我在上面的 ListBox.ItemContainerStyle 部分中上一层,就在这条线的下方:

<EventSetter Event="PreviewMouseLeftButtonDown" Handler="lb_PreviewMouseLeftButtonDown" />

我添加了这个:

<Style.Triggers>
                        <Trigger Property="IsSelected" Value="True" >
                            <Setter Property="FontWeight" Value="Bold" />
                            <Setter Property="Background" Value="Transparent" />
                            <Setter Property="Foreground" Value="Black" />
                        </Trigger>
                    </Style.Triggers>

成功了吗?不,它没有。它并没有摆脱那个蓝色的大盒子。它确实使我的字体变粗了,所以我知道它至少有这种效果。

【问题讨论】:

    标签: c# wpf listbox listboxitem


    【解决方案1】:

    您的 Thumb 仍然在 ListBoxItem 内生成,它拥有它自己的 ControlTemplate。 您需要放入您的 ListBoxItem 样式、模板的 Setter,然后只放入:

    <ControlTemplate TargetType="ListBoxItem">
        <ContentPresenter ContentSource="Content"/>
    </ControlTemplate>
    

    【讨论】:

    • 对不起,你能澄清一下这段代码应该放在哪里吗?
    • 它应该在该代码之前、之后还是在它中间的某个地方?我已经尝试了所有三个,但都没有成功。
    • 您将作为另一个 Setter 放入 ItemContainerStyle 中。您看到的蓝色边框来自默认的 ListBoxItem 模板,它们会在选择时更改其颜色。
    • 二传手>
    【解决方案2】:

    我认为这是一个“标准”的事情,wpf 在 ListBoxes 中为你做。它为您突出显示当前选择的项目。我在这里找到了一个类似的帖子:WPF ListBox, how to hide border and change selected item background color?

    他们通过将此代码添加到列表框并将该列表框的 HighlightBrushKey 设置为透明来修复它,您是否在您的情况下尝试过?

    <ListBox BorderThickness="0" HorizontalContentAlignment="Stretch"  >
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                </Style.Resources>
            </Style>
        </ListBox.Resources>                
    </ListBox>
    

    【讨论】:

    • 感谢您的努力,但这对我不起作用。
    猜你喜欢
    • 1970-01-01
    • 2013-01-23
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    相关资源
    最近更新 更多