从 ViewModel 更改 ListBox 的 SelectedItem

Change SelectedItem of ListBox from ViewModel

在 ViewModel 中设置 SelectedItem 后,如何让 ListBoxSelectedItem 高亮显示?

ItemsSource 绑定到 BarObservableCollection(该集合是 class Foo 的成员)。按钮绑定到一个命令,将一个新的空 Bar 实例添加到集合中,然后还将 SelectedItem 设置为新的空实例。

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

其他详细信息:

INotifyPropertyChanged 在基础 ViewModel class 中实现,也在 FooBar classes.

中实现

ListBox 包含用于显示 Bar 成员的自定义 ItemTemplate,以及为 [=35= 修改 Background 的自定义 ItemContainerStyle ] 触发器。

简化xaml:

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

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

简化视图模型:

private Foo _myFoo;
public Foo MyFoo
{
    get { return _myFoo; }
    set { _myFoo= value; OnPropertyChanged("MyFoo"); }
}

private Bar _selectedItem;
public Bar SelectedItem
{
    get { return _selectedItem; }
    set { _selectedItem = value; OnPropertyChanged("SelectedItem"); }
}

private void AddBar()
{
    Bar newBar = new Bar();

    MyFoo.BarCollection.Add(newBar);
    SelectedItem = newBar ;
    _unsavedChanges = true;
}

你的代码非常适合我。

注意 "Bar 3" 选中的,但是当列表框没有焦点时, 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 "Older" 意思是可以投票的年龄。


如果您的 .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>