列表框项目选择
Listbox Item selection
我有一个 WPF 项目(C#,Visual Studio 2010,MVVM)。
在这个项目中我有一个列表框。当我 select 项目时,我的对象周围出现了一个蓝色框,但我不确定该框来自何处。显然这与 selection 有关,但我不确定要改变什么才能让它消失。
列表框在这里:
<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中的'transparent',我改成红色等其他颜色来测试。它没有去掉 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>
成功了吗?不,它没有。它没有摆脱那个大蓝盒子。不过它确实让我的字体变粗了,所以我知道它至少有那种效果。
我认为这是一件 "standard" 的事情,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>
您的 Thumb 仍然是在 ListBoxItem 内部生成的,它有自己的 ControlTemplate。
你需要把你的ListBoxItem Style,一个Setter for Template,并且只放在那里:
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter ContentSource="Content"/>
</ControlTemplate>
我有一个 WPF 项目(C#,Visual Studio 2010,MVVM)。
在这个项目中我有一个列表框。当我 select 项目时,我的对象周围出现了一个蓝色框,但我不确定该框来自何处。显然这与 selection 有关,但我不确定要改变什么才能让它消失。
列表框在这里:
<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中的'transparent',我改成红色等其他颜色来测试。它没有去掉 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>
成功了吗?不,它没有。它没有摆脱那个大蓝盒子。不过它确实让我的字体变粗了,所以我知道它至少有那种效果。
我认为这是一件 "standard" 的事情,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>
您的 Thumb 仍然是在 ListBoxItem 内部生成的,它有自己的 ControlTemplate。 你需要把你的ListBoxItem Style,一个Setter for Template,并且只放在那里:
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter ContentSource="Content"/>
</ControlTemplate>