繁体   English   中英

从ViewModel更改ListBox的SelectedItem

[英]Change SelectedItem of ListBox from ViewModel

我怎样才能获得SelectedItem一个的ListBox设置后要强调SelectedItem在视图模型?

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

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

额外细节:

INotifyPropertyChanged在基础ViewModel类中实现,也在FooBar类中实现。

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

简化的xaml:

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

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

简化的viewmodel:

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 “较旧”意味着足够投票。


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

暂无
暂无

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

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