2015-07-10 74 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

我已经尝试过(我做的事情列表中的第一点),但无济于事。 –