[英]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.