- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我怎样才能得到 SelectedItem
的 ListBox
在设置 SelectedItem
后突出显示在 View 模型中?ItemsSource
绑定(bind)到 ObservableCollection
的 Bar
(集合是类 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}"/>
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;
}
最佳答案
你的代码非常适合我。
请注意,选择了“栏 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>
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/
我正在尝试将我的 ViewModel (ComboBox) 中的 SelectedItems (ListView) 和 SelectedItem 或 SelectedCategory 多重绑定(bin
一两天以来,我一直试图找出一个奇怪的 WPF 错误。我有一个 ListBox 控件,它绑定(bind)到 PolicyId 的 ObservableCollection,它是一个实现 INotifyP
我有一个 ASP DropDown,声明如下: 数据是这样填的: DropDown1.DataSource = source; DropDown1.DataTextField = "Key"; D
我有一个绑定(bind)到 ListCollectionView 的 ListBox,有一次我在列表框中选择了一个项目,然后在未来我重新创建了 ListCollectionView,但是 ListBo
我有一个简单的组合框,其中复选框作为项目。如何防止项目的实际选择。用户应该只能选中或取消选中复选框? 目前,如果我单击一个元素(而不是内容或检查本身),它将被选中。这样做的问题是:ComboBox 的
有没有办法知道在 Windows 资源管理器中选择了哪个文件?我一直在看这里发布的教程 Idiots guide to ...但描述的行动是: 徘徊 语境 菜单属性 拖 拖放 我想知道是否有在选择文件
我有一个 WPF 数据网格。 DataGrid 绑定(bind)到 IList。该列表有许多项目,因此 DataGrid MaxHeight 设置为预定义值,并且 DataGrid 自动显示滚动条。选
好的,已经使用 WPF 有一段时间了,但我需要一些帮助。 我有一个 ComboBox如下所示: 每当我离开选项卡 1 然后回到它时,选择就会被删除。
示例代码如下: ddlCat.Items.Insert(0, new ListItem("Item1", "1")); ddlCat.Items.Insert(1, new ListItem("It
我想使用 SelectedItem 将选择设置为代码中的组合框。 我只能通过使用 SelectedValue 让它工作。 SelectedItem 将在堆栈跟踪的顶部抛出一个空引用异常: 在 Atta
这是我在这个很棒的平台上的第一个问题,是的,我在这里搜索了如何执行此操作,在我的情况下很少见,因为它应该可以工作,但我从 SelectedItem 中得到的只是一个字符串(它的内容,这是一个 Text
我有一个ListBox,其中包含填充TextBoxes 的项目。当从 ListBox 中进行选择时,如何识别选择的文本字符串。这是我的 ListBox XAML 代码:
我有一个列表框,其中填充了来自 ImageDomainService(RIA 服务)的列表。我想从 ListBox 中选择一张图像并在其旁边显示该图像的较大版本。图像单独存储在/images/文件夹中
我有一个绑定(bind)到 ObservableCollection 的 ListView ItemsSource。我通过 MVVM 添加了一个属性来跟踪 ListView.SelectedItem。
我正在尝试从 QTableView 小部件(下面复制的片段)返回选定行的向量,但是返回的值与选择不对应,我相信我不了解 QModelIndexList/QModelIndex 与 QTableView
我试图寻找答案,但我没有任何运气。基本上我有一个 ListView ,它绑定(bind)到从 View 模型返回的集合。我将 ListView 的选定项目绑定(bind)到我的 ListView 中的
我有一个实现 INotifyPropertyChanged 的 View 模型.在这个 viewModel 上有一个名为 SubGroupingView 的属性。 .此属性绑定(bind)到组合框的选
我有一个 ComboBox,它的 ItemsSource 绑定(bind)到静态 List的选项。 ComboBox 是绑定(bind)到 CustomObject 类的表单的一部分,该类的属性之一是
我有一个将 SelectedItem 绑定(bind)到 ViewModel 的 ComboBox。 当用户在 View ComboBox 中选择一个新项目时,我想显示一个提示并验证他们是否要进行更
我有一个 ViewModel(其结构的伪代码): class ViewModel { public List Packages { get; set; } } enum Type {
我是一名优秀的程序员,十分优秀!