[英]Change SelectedItem of ListBox from ViewModel
我怎样才能获得SelectedItem
一个的ListBox
设置后要强调SelectedItem
在视图模型?
ItemsSource
绑定到Bar
的ObservableCollection
(该集合是类Foo
的成员。一个按钮绑定到一个命令,该命令将新的空Bar
实例添加到集合中,然后还将SelectedItem
设置为新的空实例。
将实例添加到集合后, ListBox
将更新以显示新的空白Bar
。 但是,在ViewModel中设置SelectedItem
属性后,新实例不会在ListBox
突出显示,但它正在设置并且引发了PropertyChanged
事件( SelectedItem
显示在View中的其他位置)。
额外细节:
INotifyPropertyChanged
在基础ViewModel类中实现,也在Foo
和Bar
类中实现。
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.