gpt4 book ai didi

c# - 从 ViewModel 更改 ListBox 的 SelectedItem

转载 作者:行者123 更新时间:2023-12-03 10:43:10 32 4
gpt4 key购买 nike

我怎样才能得到 SelectedItemListBox在设置 SelectedItem 后突出显示在 View 模型中?
ItemsSource绑定(bind)到 ObservableCollectionBar (集合是类 Foo 的成员。一个按钮绑定(bind)到一个命令,该命令将一个新的空 Bar 实例添加到集合中,然后还将 SelectedItem 设置为新的空实例。

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

额外细节:
INotifyPropertyChanged在基础 ViewModel 类中实现,也在 Foo 中实现和 Bar类。
ListBox包含自定义 ItemTemplate显示 Bar成员和自定义 ItemContainerStyle修改 Background对于IsMouseOver扳机。

简化的xml:

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

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

简化 View 模型:
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;
}

最佳答案

你的代码非常适合我。

enter image description here

请注意,选择了“栏 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>

关于c# - 从 ViewModel 更改 ListBox 的 SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44292342/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com