【问题标题】:Disable the FocusStyle of the ListViewItem Element in a ListView in c#在c#中禁用ListView中ListViewItem元素的FocusStyle
【发布时间】:2019-06-19 06:16:51
【问题描述】:

我在运行时生成一个 ListView 并不断填充它。问题是我想在将鼠标悬停在一个项目上或选择它时禁用颜色变化。

每个 ListViewItem 都不是可聚焦的。这样颜色变化不会停留,仅在悬停在项目上时才会显示。 我尝试通过制作我的一种样式来禁用它,但不幸的是它没有按预期工作。

我用过这个样式

Setter setter = new Setter()
{
    Property = Control.BackgroundProperty,
    Value = null
};
Setter setter1 = new Setter()
{
    Property = Control.BorderBrushProperty,
    Value = null
};

Trigger trigger = new Trigger()
{
     Property = ListBoxItem.IsSelectedProperty,
     Value = true,
     Setters = { setter, setter1 }
};

Style style = new Style()
{
     Triggers = { trigger }
};

我也尝试将 ListView.FocusVisualStyle 设置为 null,但也没有结果。

【问题讨论】:

  • listview可以设置Enabled=false
  • 我建议使用Transparent 而不是null 作为值。
  • @irategenius setting enabled = false 也会禁用滚动功能
  • @GaurangDave 我应该在哪里应用样式。我尝试了 ListViewItem.FocusVisualStyle、ListView.ItemContainerStyle 和 ListView.FoucsVisualStyle。他们中的任何一个都没有改变任何东西

标签: c# wpf xaml listview


【解决方案1】:

有两个选择

1) 如果您不需要选择项目:将 IsHitTestVisible 设置为 false

2) 打造自己的风格

<!--Default LitsView-->
<Style TargetType="ListView">
    <Setter Property="Background" Value="{StaticResource YOUR_BACKGROUND_COLOR}"/>
</Style>

<!--Default LitsViewItem-->
<Style x:Key="FocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="Padding" Value="4,1"/>
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd">
                            <Setter.Value>
                                <SolidColorBrush Color="{StaticResource YOUR_COLOR}" Opacity="0.3"/>
                            </Setter.Value>
                        </Setter> 
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource YOUR_COLOR}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="True"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource YOUR_COLOR}"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource YOUR_COLOR}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

【讨论】:

  • 只设置 IsHitTestVisible=false 对我有用。谢谢
  • @CaptainLama 您可以在运行时使用 xaml,只需在您的代码中执行 smt.Style = Application.Current.FindResource("YOUR_STYLE_KEY") as Style
【解决方案2】:

在你的 xaml 代码中试试这个...

<ListView ...>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

【讨论】:

  • 我不能使用 xaml,因为一切都是在运行时生成的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 2011-06-26
  • 2022-01-22
  • 2013-12-30
  • 2011-11-02
相关资源
最近更新 更多