2013-10-07 56 views
0

我有一個ListView顯示對象的集合。我想在單擊ListViewItem時禁用「藍色效果」或「藍色矩形」,並且只允許啓用此項中的對象。有沒有人有這個想法?在WPF中禁用控件的選擇背景效果

這是我的ListViewItems中的一個的實施例:

LisViewItem

這是我的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> 
+0

您必須修改控件模板。 – Tico

+0

怎麼樣,你能解釋更多嗎? – NTinkicht

回答

2

父控制的IsEnabled屬性設置爲False會自動IsEnabled設置每個子控件的屬性也爲False。唯一的解決方法是將要禁用的單個控件的IsEnabled屬性設置爲False,而不是父控件的

但是,您可以創建一個bool屬性Bind到所有相關的控制的IsEnabled屬性,以便至少,您可以禁用和使用一個屬性全部啓用:

<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屬性的控件纔會受到影響。

UPDATE >>>

因此,原來你真的想刪除ListView默認選擇的顏色......那就是你「出現」在問有很大不同。不過,現在我知道,你可以用一個簡單的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> 

你只需要這些來隱藏顏色之一,但我向您展示以供將來參考休息。你可以這樣使用它:

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

對不起@Sheridan!我想我把錯誤的ListView,我更新了帖子!問題是:正如您在我作爲示例放置的圖像中所看到的,我不希望單擊listeViewItem後出現「藍色填充」。所以我嘗試了「IsEnabled = False」,但CheckBox也被禁用了!怎麼做 ?? – NTinkicht

+0

哦,你是否在談論默認的「背景」選擇顏色(所選項目)? – Sheridan

+0

yeess!而已 ! – NTinkicht

1

正如我在評論中所述,我將更改ListViewItem模板。
代碼非常冗長,所以我們來做:

首先,創建ResourceDictionary並在其中設置對它的引用App.xaml
已經這樣做了,粘貼如下代碼在ResourceDictionary中:

<SolidColorBrush x:Key="Item.MouseOver.Background" Color="#1F26A0DA"/> 
    <SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/> 
    <SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA"/> 
    <SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA"/> 
    <SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/> 
    <SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/> 
    <Style x:Key="ListViewItemStyle1" 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" Value="{StaticResource Item.MouseOver.Background}"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/> 
         </MultiTrigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="Selector.IsSelectionActive" Value="False"/> 
           <Condition Property="IsSelected" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/> 
         </MultiTrigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="Selector.IsSelectionActive" Value="True"/> 
           <Condition Property="IsSelected" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/> 
         </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> 

注意,前6個標籤(SolidColorBrush)根據ListViewItem的狀態必須的顏色的參考文獻。
從這裏,你可以採取2條路徑,將SolidColorBrush顏色更改爲任何你想要的,在你的情況下Transparent或完全刪除Triggers(不推薦)。

完成後,將ListBoxItem的Style屬性設置爲您指定的x:Key。代碼中的名稱是x:Key="ListViewItemStyle1"

我希望這個指導你!

+0

非常感謝@Tico,這段代碼對管理控制的所有效果非常有幫助! – NTinkicht

+0

WPF是非常可定製的,我喜歡它!太糟糕了,我沒有得到答案! – Tico

+0

我希望我也能給你答案!因爲你的工作也很棒!如果我需要幫助,希望你能來到這裏!謝謝 – NTinkicht