【发布时间】:2015-09-28 22:09:06
【问题描述】:
我想设置禁用 ListView 的样式。但无论我做什么,listView 在禁用时都保持默认样式。
我已经尝试过的:
- 根据此线程将系统颜色
SystemColors.ControlBrushKey设置为Transparent:Change disabled listbox background to gray - 我也在MSDN查看了listView的样式,重新定义了
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; }
}
我只希望 listView 在禁用时具有红色背景。我怎样才能做到这一点?
【问题讨论】: