C# 对CustomListbox中的选定项目禁用鼠标悬停和选择

C# 对CustomListbox中的选定项目禁用鼠标悬停和选择,c#,wpf,listbox,wpf-style,C#,Wpf,Listbox,Wpf Style,我创建了一个自定义列表框,基于bool,我想对一些项目禁用鼠标悬停和鼠标选择。我使用了Style类并在CustomListBox的代码中设置了样式。我的代码如下: public class DragDropListBox : ListBox { public DragDropListBox() { Style itemContainerStyle = new Style(typeof(ListBoxItem)); itemConta

我创建了一个自定义列表框,基于bool,我想对一些项目禁用鼠标悬停和鼠标选择。我使用了Style类并在CustomListBox的代码中设置了样式。我的代码如下:

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>

您需要修改或替换
ListBoxItem
控制模板
,并将其应用到您的
样式中。对于标志本身,您需要一些bool属性来表示该值,以便在模板中使用它。我建议使用继承的附加属性,您可以在
DragDropListBox
上设置该属性,然后访问子
ListBoxItems


您绝对应该在XAML中完成这个模板工作。在代码中编写
ControlTemplate
充其量是一件乏味的事情,它会阻止您使用现有模板作为起点。如果希望在代码中保留事件连接之类的内容,可以从参考资料中提取模板XAML,然后将其分配给代码中的setter。

我知道这一点。看一看编辑好的问题。