在WPF中禁用控件的选择背景效果 [英] Disable Control's Selection Background Effect in WPF

查看:103
本文介绍了在WPF中禁用控件的选择背景效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,它显示对象的集合.我想在单击ListViewItem时禁用蓝色效果"或蓝色矩形",并仅启用该项目中的对象.有人对此有想法吗?

I have a ListView which shows a collection of objects. I want to disable the "Blue effect" or "Blue rectangle" when clicking on the ListViewItem, and Let only the object in this Item enabled. Does anyone has an idea about that ??

这是我的一个ListViewItems的示例:

Here is an Example of one of my ListViewItems:

这是我的listView:

Here is my listView:

<ListView SelectionMode="Single" IsManipulationEnabled="True" HorizontalAlignment="Left" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding ChildrenList}" Background="Transparent" BorderBrush="Transparent" >
        <ListView.LayoutTransform>
            <RotateTransform Angle="{Binding IsVertical, Converter={StaticResource AngleOfBool}}"></RotateTransform>
        </ListView.LayoutTransform>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Stretch" Background="Transparent" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <CheckBox   Content="{Binding Id}"  Height="auto" Name="Flag" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="0" IsChecked="{Binding IsVisible}"/>
                    <Grid Grid.Column="1">
                        <area:Area HorizontalAlignment="Stretch" Visibility="{Binding IsVisible, Converter={StaticResource VisibilityOfBool}}" />
                    </Grid>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

推荐答案

将父控件的IsEnabled属性设置为False也会自动将每个子控件的IsEnabled属性设置为False.解决此问题的唯一方法是将要禁用的单个控件的IsEnabled属性设置为父控件的False,而不是 .

Setting the IsEnabled property of a parent control to False will automatically set the IsEnabled property of every child control to False as well. The only way around this is to set the IsEnabled property of the individual controls that you want to be disabled to False, instead of that of the parent control.

但是,您可以在所有相关控件的IsEnabled属性中创建一个bool属性到Bind,以便至少可以使用一个属性禁用和启用所有这些控件:

However, you can create one bool property to Bind to the IsEnabled property of all of the relevant controls so that at least, you can disable and enable them all using the one property:

<Grid Name="Parent">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"></ColumnDefinition>
        <ColumnDefinition Width="auto"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ContentControl IsEnabled="AreInnerControlsEnabled" Grid.Column="0" Content="{Binding}" Visibility="{Binding IsVisible, Converter= {StaticResource VisibilityOfBool} }"></ContentControl>
    <Grid Grid.Column="1" Visibility="{Binding IsVisible, Converter= {StaticResource VisibilityOfBool} }" HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="0.5*"></RowDefinition>
            <RowDefinition Height="0.5*"></RowDefinition>
        </Grid.RowDefinitions>
        <Label IsEnabled="AreInnerControlsEnabled" Grid.Row="0" Background="Transparent" Content="{Binding Top}" VerticalAlignment="Top" HorizontalAlignment="Stretch"></Label>
        <Label Grid.Row="1" Background="Transparent" Content="{Binding Bottom}" VerticalAlignment="Bottom" HorizontalAlignment="Stretch"></Label>
    </Grid>
</Grid>

使用此方法,仅使用AreInnerControlsEnabled属性的控件将受到影响.

Using this method, only the controls using the AreInnerControlsEnabled property will be affected.

更新>>>

因此,事实证明您实际上是要删除ListView的默认选择颜色...与您看上去"要询问的颜色有很大不同.但是,现在我知道了,您可以通过使用简单的Style:

So it turns out that you actually want to remove the default selection colour of the ListView... that is very different to what you 'appeared' to be asking. However, now I know that, you can achieve that very easily by using a simple Style:

<Style x:Key="HiddenDefaultSelectionStyle" TargetType="{x:Type ListViewItem
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
    </Style.Resources>
</Style>

您只需要其中一种即可隐藏该颜色,但我向您展示了其余的颜色,以供将来参考.您可以像这样使用它:

You only need one of these to hide that colour, but I have shown you the rest for future reference. You can use it like this:

<ListView ItemContainerStyle="{StaticResource HiddenDefaultSelectionStyle}" ... />

这篇关于在WPF中禁用控件的选择背景效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆