我创建了一个自定义列表框,并基于一个布尔值,我想为几个项目禁用鼠标悬停和鼠标选择。我使用了 Style 类并在自定义列表框的代码中设置了样式。我的代码如下:
public class DragDropListBox : ListBox
{
public DragDropListBox()
{
Style itemContainerStyle = new Style(typeof(ListBoxItem));
itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.DropEvent, new DragEventHandler(ListBoxItemDropHandler)));
this.ItemContainerStyle = itemContainerStyle;
}
}
现在我已经为 drop 事件设置了一个事件设置器并且工作正常。如何设置样式以禁用基于标志的项目的鼠标悬停和鼠标选择效果?我发现的大部分代码都在 XAML 中。但我需要自定义列表框的代码。任何帮助,将不胜感激。
到目前为止,这种风格对我有用,但它在 XAML 中如何转换它 C# 代码,特别是视觉状态和情节提要..
<Style x:Key="ListBoxItemStyleTransparentSelect" TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
<EventSetter Event="Drop" Handler="listbox1_Drop"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"/>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="fillColor" IsHitTestVisible="True" Opacity="0" RadiusY="1" RadiusX="1"/>
<Rectangle x:Name="fillColor2" IsHitTestVisible="True" Opacity="0" RadiusY="1" RadiusX="1"/>
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/>
<Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" StrokeThickness="1" Visibility="Collapsed"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>