我想设置禁用ListView的样式。但无论我做什么,禁用时listView都保持默认样式。更改禁用列表背景查看
我已经尝试过:根据这个线索
- 设置系统颜色
SystemColors.ControlBrushKey
到Transparent
:Change disabled listbox background to gray - 我也看了一下ListView控件的样式在MSDN并重新定义了
DisabledBorderLightColor
- 当然我创建了一个触发器
所有这些尝试可以在窗口的资源部分可以看出:
<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; }
}
我只想列表视图有红色的背景时,它是禁用。我怎样才能做到这一点?
可否请您提供'Microsoft_Windows_Themes'的'xmlns ='-statement。我真的不得不添加对'PresentationFramework.Aero.dll'的引用吗? –
或者你可以用普通边框代替镶边,并为MouseOver行为和所有你自己添加触发器。请记住,WPF控件是无形的,并且ListView没有明确支持自定义禁用的背景。这只是控件如何由默认主题模板化的问题,并且您希望覆盖该控件......因此,您必须在所有特效中创建一个新的模板。我刚刚使用默认的W7 Aero模板作为基础,但完全取决于您。 – almulo
或...您可以尝试覆盖它使用的系统刷...但是再一次,控件使用的刷子完全取决于主题。重写笔刷毕竟是一种黑客。这就是为什么我提出这个建议,因为它是在WPF中唯一“正确”的方式。 – almulo