WPF:数据绑定ListView控件使列表视图项的前景色反映布尔属性值更改 [英] WPF: databinding to ListView so that listview item's ForeColor reflects bool property value changes

查看:363
本文介绍了WPF:数据绑定ListView控件使列表视图项的前景色反映布尔属性值更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF应用程序,.NET 3.5。

我有一个的ListView 控制,这是我结合业务类的集合。

I have a ListView control, to which I bind a collection of business classes.

我想绑定的前景色项目,以类的布尔属性(比如:MyClass的有效),使项目显示为黑色当活跃,浅灰色时被禁止。

I'd like to bind the ForeColor of items to a bool property of the class (say: MyClass.Active), so that the items are displayed in black when active, in light gray when disabled.

我想列表视图项目改变它们的颜色上即时,我的意思是,当我在我的数据绑定集合更改有效的实例之一的价值,其相应的列表视图项将改变其颜色自动。

I want listview items to change their color on-the-fly, I mean when I'd change Active value of one of the instances in my databound collection, its respective listview item would change its color automatically.

我需要做什么来实现这一目标?我发现在WPF数据绑定许多教程在网上,在计算器上的一些问题,但不完全一样的东西,我不想要开始我的解决方案结合了一切,我可以把我的手,通过试验和错误

What do I need to achieve this? I've found numerous tutorials on WPF databinding on the net, some questions on StackOverflow, but not exactly the same thing, and I don't want to start combining my solution out of everything I can put my hands on, by trial and error.

我知道的 INotifyPropertyChanged的(为我的企业级),的IValueConverter (但应我需要它的布尔?), DataTrigger 等等。

I know about INotifyPropertyChanged (for my business class), IValueConverter (but should I need it for a bool??), DataTrigger etc.

但该拼图做我真正需要的,什么是实现我的目标simpliest方式?

But which pieces of the puzzle do I really need, what is the simpliest way to achieve my goal?

推荐答案

我只想做一个风格,并适用于你的列表项目的风格。提供了你的对象实现 INotifyPropertyChanged的和属性更改事件被引发时IsActive的变化,这将改变前台到格雷如果 IsActive =假

I would just do it in a style, and apply that style on your List items. Providing that your object implements INotifyPropertyChanged and the property changed event gets raised when IsActive changes, this will change the foreground to Gray if IsActive = False

<Style x:Key="DisableInactiveTextStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground" Value="Black" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsActive}" Value="False">
            <Setter Property="Foreground" Value="LightGray" />
        </DataTrigger>
    </Style.Trigger>
</Style>

<TextBlock Style="{StaticResource DisableInactiveTextStyle}" ... />

这篇关于WPF:数据绑定ListView控件使列表视图项的前景色反映布尔属性值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆