如果选择了列表框项,如何设置viewmodel属性?

时间:2012-03-07 17:12:45

标签: wpf properties viewmodel listboxitem selected

我有一个列表框...它被绑定到类型(类)“A”的项目列表。类“A”具有布尔属性“IsCurrentViewActive”。默认情况下这是错误的。当用户在列表框中选择一个项目时....我需要将相应项目的“IsCurrentViewActive”属性设置为TRUE。

我在列表框中尝试了以下代码...但它无法正常工作。任何帮助都将不胜感激。

<Style x:Key="ListBoxItemTabStyle2Extended" TargetType="{x:Type ListBoxItem}" >
                <Setter Property="IsSelected" Value="{Binding Path=IsCurrentViewActive, Mode=TwoWay}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="True">
                    <Setter Property="IsSelected" Value="{Binding Path=IsCurrentViewActive, Mode=TwoWay}"/>
                </DataTrigger>                    
                </Style.Triggers>
            </Style>

编辑:我们可以这样做还是需要使用附属物?

1 个答案:

答案 0 :(得分:0)

上面的XAML实际上对我来说很好。我认为这只是使上下文正确的问题。我创建了一个示例,您可以在可以正常工作的地方下载该示例。我认为差异可能是在我的版本中,我将样式存储在ListBox的父控件的资源中。我认为您不能将它们存储在ListBox的资源中。

https://www.dropbox.com/s/s64kp9p0b2w1imk/ListBoxMenu.7z?dl=0