2015-07-10 42 views
2

我想設置禁用ListView的樣式。但無論我做什麼,禁用時listView都保持默認樣式。更改禁用列表背景查看

我已經嘗試過:根據這個線索

  1. 設置系統顏色SystemColors.ControlBrushKeyTransparentChange disabled listbox background to gray
  2. 我也看了一下ListView控件的樣式在MSDN並重新定義了DisabledBorderLightColor
  3. 當然我創建了一個觸發器

所有這些嘗試可以在窗口的資源部分可以看出:

<Window.Resources> 
    <Style TargetType="{x:Type ListView}"> 
     <Style.Resources> 
      <Color x:Key="DisabledBorderLightColor">#FF00FF00</Color> 
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/> 
     </Style.Resources> 
     <Setter Property="Background" Value="Green"/> 
     <Style.Triggers> 
      <Trigger Property="IsEnabled" Value="False"> 
       <Setter Property="Background" Value="Red"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="25"/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <Button Content="Unselect" Click="UnselectItem"></Button> 
    <ListView x:Name="_listView" 
       Grid.Row="1" 
       d:DataContext="{d:DesignData ObjectToShow}" 
       IsEnabled="True" 
       SelectionChanged="SelectItem"> 
     <ListView.View> 
      <GridView> 
       <GridView.Columns> 
        <GridViewColumn Header="Header1" DisplayMemberBinding="{Binding Property1}"/> 
        <GridViewColumn Header="Header2" DisplayMemberBinding="{Binding Property2}"/> 
       </GridView.Columns> 
      </GridView> 
     </ListView.View> 
    </ListView> 
</Grid> 

這是根據C#:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     var objectsToShow = new List<ObjectToShow> 
     { 
      new ObjectToShow 
      { 
       Property1 = "Content1Property1", 
       Property2 = "Content1Property2" 
      }, 
      new ObjectToShow 
      { 
       Property1 = "Content2Property1", 
       Property2 = "Content2Property2" 
      }, 
     }; 

     _listView.ItemsSource = objectsToShow; 
    } 

    private void SelectItem(object sender, SelectionChangedEventArgs e) 
    { 
     _listView.IsEnabled = false; 
    } 

    private void UnselectItem(object sender, RoutedEventArgs e) 
    { 
     _listView.SelectedItem = null; 
     _listView.IsEnabled = true; 
    } 
} 

public class ObjectToShow 
{ 
    public string Property1 { get; set; } 

    public string Property2 { get; set; } 
} 

我只想列表視圖有紅色的背景時,它是禁用。我怎樣才能做到這一點?

回答

1

ListView改變風格是這樣的:

<Style TargetType="{x:Type ListView}"> 
     <Setter Property="Background" Value="Green"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListView}"> 
        <Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true"> 
         <ScrollViewer Padding="{TemplateBinding Padding}" Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}"> 
          <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </ScrollViewer> 
        </Microsoft_Windows_Themes:ListBoxChrome> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsGrouping" Value="true"> 
          <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <!-- Here comes your custom disabled background color --> 
          <Setter Property="Background" TargetName="Bd" 
            Value="Red"/> 
          <!-- Here comes your custom disabled background color --> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

,當你有這實在是太煩人覆蓋整個控件的模板,只是爲了改變它的一小部分......但有時你沒有選擇。

在這種情況下,由於更改背景顏色的觸發器位於ControlTemplate中,因此無法使用樣式觸發器覆蓋它。

+1

可否請您提供'Microsoft_Windows_Themes'的'xmlns ='-statement。我真的不得不添加對'PresentationFramework.Aero.dll'的引用嗎? –

+0

或者你可以用普通邊框代替鑲邊,併爲MouseOver行爲和所有你自己添加觸發器。請記住,WPF控件是無形的,並且ListView沒有明確支持自定義禁用的背景。這只是控件如何由默認主題模板化的問題,並且您希望覆蓋該控件......因此,您必須在所有特效中創建一個新的模板。我剛剛使用默認的W7 Aero模板作爲基礎,但完全取決於您。 – almulo

+0

或...您可以嘗試覆蓋它使用的系統刷...但是再一次,控件使用的刷子完全取決於主題。重寫筆刷畢竟是一種黑客。這就是爲什麼我提出這個建議,因爲它是在WPF中唯一「正確」的方式。 – almulo

0

我想你必須定義你自己的控件模板覆蓋這個默認的ListView模板。

或者你可以嘗試,如果內置的控制刷確實你需要:

<ListView.Style> 
    <Style TargetType="{x:Type ListView}"> 
     <Style.Resources> 
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Crimson"/> 
     </Style.Resources> 
    </Style> 
</ListView.Style> 
+0

我已經嘗試過(我做的事情列表中的第一點),但無濟於事。 –