从ViewModel更改ListBox的SelectedItem

时间:2017-05-31 18:35:18

标签: c# wpf mvvm .net-4.0 selecteditem

如何在ViewModel中设置SelectedItem后突出显示ListBox的{​​{1}}?

SelectedItem绑定到ItemsSource ObservableCollection(该集合是类Bar的成员。按钮绑定到添加新命令的命令将Foo实例清空到集合中,然后将Bar设置为新的空实例。

将实例添加到集合后,SelectedItem会更新以显示新的空白ListBox。但是,在ViewModel中设置Bar属性后,新实例未在SelectedItem中突出显示,但正在设置并且ListBox事件被引发(PropertyChanged显示在视图中的其他位置。

其他详情:

SelectedItem在基础ViewModel类中实现,也在INotifyPropertyChangedFoo类中实现。

Bar包含显示ListBox成员的自定义ItemTemplate,以及修改Bar触发器的ItemContainerStyle的自定义Background

简化的xaml:

IsMouseOver

简化的viewmodel:

<ListBox ItemsSource="{Binding Path=MyFoo.BarCollection}"
         SelectedItem="{Binding Path=SelectedItem, 
                       UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

<Button Content="Add New Bar"
        Command="{Binding Path=AddBarCommand}"/>

1 个答案:

答案 0 :(得分:0)

您的代码对我来说非常合适。

enter image description here

请注意&#34; Bar 3&#34;选择了 ,但是当列表框没有焦点时,Windows 7上的默认主题非常难以让您注意不到。而Windows 7甚至不是最糟糕的。我们的应用程序使用WhiteSmoke作为默认背景,并且我们的老用户 1 无法判断是否选择了列表框项目时遇到了重大问题。

幸运的是,这是一个微不足道的修复。这些资源可以很容易地在Window.Resources,全局ListBox样式或App.xaml中。它取决于你想要应用它们的广泛程度。

<ListBox 
    ItemsSource="{Binding MyFoo.BarCollection}"
    SelectedItem="{Binding SelectedItem}"
    >
    <ListBox.Resources>
        <SolidColorBrush 
            x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" 
            Color="{x:Static SystemColors.HighlightColor}"
            Opacity="0.5"
            />
        <SolidColorBrush 
            x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" 
            Color="{x:Static SystemColors.HighlightTextColor}"
            />
    </ListBox.Resources>
</ListBox>

1 &#34;年龄较大&#34;意思是足够投票。

如果您的.NET版本早于SystemColors.InactiveSelectionHighlightTextBrushKey的存在,这可能会使用一些改进,但它可以工作:

<ListBox
    >
    <ListBox.ItemContainerStyle>
        <Style 
            TargetType="ListBoxItem" 
            BasedOn="{StaticResource {x:Type ListBoxItem}}"
            >
            <Style.Setters>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Grid
                                Background="{TemplateBinding Background}"
                                >
                                <ContentPresenter />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter 
                        Property="Background" 
                        Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}" 
                        />
                    <Setter 
                        Property="Foreground" 
                        Value="{StaticResource {x:Static SystemColors.HighlightTextBrushKey}}" 
                        />
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected" Value="True" />
                        <Condition Property="IsEnabled" Value="False" />
                    </MultiTrigger.Conditions>
                    <Setter 
                        Property="Background" 
                        Value="{StaticResource {x:Static SystemColors.InactiveCaptionBrushKey}}" 
                        />
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
相关问题