简体   繁体   English

c#wpf,属性的绑定值取决于布尔值

[英]c# wpf, binding value of property depending on the bool value

Sorry for my bad english. 对不起,我的英语不好。 I want to bind a BorderBrush of ListBoxItem depending on the bool value of object. 我想根据对象的布尔值绑定ListBoxItemBorderBrush I have a Directory class, that has IsForCopy and IsCutted bool props. 我有一个Directory类,它具有IsForCopyIsCutted bool道具。 So, if IsForCopy is true, then BorderBrush = Red, if IsCutted is true, then BorderBrush = Blue. 因此,如果IsForCopy为true,则BorderBrush =红色,如果IsCutted为true,则BorderBrush =蓝色。 How can I do this? 我怎样才能做到这一点?

ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                    <EventSetter Event="MouseDoubleClick" Handler="SecondListBoxItem_MouseDoubleClick"/>
                    <Setter Property="BorderBrush" Value="What do I should type there?"></Setter>
                </Style>
            </ListBox.ItemContainerStyle>

You need a class that implements IValueConverter. 您需要一个实现IValueConverter的类。 Then in the convert method you can specify how you want to map a bool to a brush any way you want. 然后,在convert方法中,您可以指定如何将布尔值映射到笔刷。 And every binding has a converter attribute which you can set to this class that implemented IValueConverter. 每个绑定都有一个转换器属性,您可以将其设置为实现IValueConverter的此类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM