WPF绑定的ListBox获取ListBoxItem及GoToState应用

var listBoxItem = ListBox1.Items[] as List1BoxItem;
这时提示listBoxItem为null,那该如何获取到ListBoxItem呢?这时就用到了ItemContainerGenerator.ContainerFromIndex(int index)方法,该方法返回对应于 System.Windows.Controls.ItemCollection 中指定索引项的元素:
var listBoxItem = ListBox1.ItemContainerGenerator.ContainerFromIndex() as FrameworkElement;
这次就获取到了ListBox1中第一个listBoxItem。
2.WPF中的手动控制控件外观变化
如何在不移动鼠标的情况下触发ListBoxItem的MouseOver状态发生呢?这时就利用到了VisualStateManager.GoToState(FrameworkElement control, string stateName, bool useTransitions)方法。该方法可以使控件在两个状态间随意转换:
首先,为项目中的ListBoxItem自定义一个Style,其包含Normal和MouseOver两种状态:
<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
<EasingColorKeyFrame KeyTime="0" Value="#FF5CFD30"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
<EasingColorKeyFrame KeyTime="0" Value="#FF29B5B8"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="rectangle" Fill="#FF5AFB2E" Stroke="Black"/>
<TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="{TemplateBinding Content}" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
为ListBox1应用样式,绑定数据源,拖出两个button分别用于设置ListBoxItem的状态:
<Grid>
<ListBox x:Name="ListBox1" HorizontalAlignment="Left" Height="100" Margin="57,45,0,0" VerticalAlignment="Top" Width="100" ItemsSource="{Binding MenuList}" ItemContainerStyle="{DynamicResource ListBoxItemStyle1}" d:LayoutOverrides="HorizontalAlignment, VerticalAlignment"/>
<Button x:Name="btnMouseOver" Content="MouseOver" HorizontalAlignment="Left" Margin="75,0,0,65.163" VerticalAlignment="Bottom" Width="75" Click="btnMouseOver_Click"/>
<Button x:Name="btnNormal" Content="Normal" Margin="213,0,221,65.163" VerticalAlignment="Bottom" Click="btnNormal_Click"/>
</Grid>
后台找到ListBoxItem并改变其外观:
private void btnMouseOver_Click(object sender, RoutedEventArgs e)
{
var item = ListBox1.ItemContainerGenerator.ContainerFromIndex() as FrameworkElement;
VisualStateManager.GoToState(item, "MouseOver", false);
} private void btnNormal_Click(object sender, RoutedEventArgs e)
{
var item = ListBox1.ItemContainerGenerator.ContainerFromIndex() as FrameworkElement;
VisualStateManager.GoToState(item, "Normal", false);
}
效果:


WPF绑定的ListBox获取ListBoxItem及GoToState应用的更多相关文章
- WPF ListBox 获取listBoxItem
1.已知item的DataContext,获取ListBoxItem 1)ItemContainerGenerator.ContainerFromItem var selectedItem = Doc ...
- WPF,解决Listbox,按住ListboxItem向下拖出Listbox,横向滚动条跑到最后。
类似这种样式的控件,.,在横向滚动条隐藏的情况下有这样的问题.(横向滚动条显示的时候也会,,目前不知道怎么解决.) 因为这个控件偏移是利用ListBox的ItemsPanelTemplate模版里的S ...
- 获取listboxitem在ListBox中的index并转换成abcd
原文 获取listboxitem在ListBox中的index并转换成abcd 截图如下: 1.实现Converter 获取到listbox,并得到listitem在listbox中的index p ...
- wpf之ListBox中ListBoxItem横向排列
ListBox中ListBoxItem默认是纵向排列,可以通过自定义样式,让其横向排列, 如下Demo: XAML: <Window x:Class="ListBoxItemStyle ...
- WPF快速入门系列(4)——深入解析WPF绑定
一.引言 WPF绑定使得原本需要多行代码实现的功能,现在只需要简单的XAML代码就可以完成之前多行后台代码实现的功能.WPF绑定可以理解为一种关系,该关系告诉WPF从一个源对象提取一些信息,并将这些信 ...
- WPF 简易手风琴 (ListBox+Expander)
概述 之前听说很多大神的成长之路,几乎都有个习惯--写博文,可以有效的对项目进行总结.从而提高开发的经验.所以初学WPF的我想试试,顺便提高一下小学作文的能力.O(∩_∩)O哈哈~ 读万卷书不如行万里 ...
- wpf CollectionViewSource与ListBox的折叠、分组显示,及输入关键字 Filter的筛选
在wpf中虽然ObservableCollection<T>作为ListBox的Itemsource,很好,很强大!但是CollectionViewSource与ListBox才是天作之合 ...
- WPF:自定义ListBox的选择样式
首先介绍一种简单地方法:就是通过自定义SystemColors类的参数来自定义WPF ListBox选择颜色的, SystemColors的HighlightBrushKey和HighlightTex ...
- 解决WPF程序中ListBox ItemsSource变化时不重置ScrollBar的问题
解决WPF程序中ListBox ItemsSource变化时不重置ScrollBar的问题 当我们改变ListBox的ItemsSource时,会发现这样一个问题:数据源变化时,虽然控件中的内容会跟着 ...
随机推荐
- Codeforces821C Okabe and Boxes 2017-06-28 15:24 35人阅读 评论(0) 收藏
C. Okabe and Boxes time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- QT5.4.0安装以及与VS2010整合安装---64bit操作系统解决方案
QT5.4.0安装以及与VS2010整合安装---64bit操作系统解决方案 注意,目前QT官网不能下载,必须提供注册,然后才可以下载. 网上不同版本安装的细节有差异,特将我的安装相关操作贴出来,希望 ...
- 面向对象的设计原则(JAVA)
一.单一职责原则(Single Responsibility Principe,SRP) 1.1单一职责原则的定义 1)定义:在软件系统中,一个类只负责一个功能领域中的相应职责. 2)另一种 ...
- cxgrid强大用法
cxgrid强大用法 (2012-07-25 14:09:42) 转载▼ 标签: delphi cxgrid 用法 强大 杂谈 分类: Delphi cxGrid功能强大,适合做企业级的复杂查询.非常 ...
- caffe 每层结构
如何在Caffe中配置每一个层的结构 最近刚在电脑上装好Caffe,由于神经网络中有不同的层结构,不同类型的层又有不同的参数,所有就根据Caffe官网的说明文档做了一个简单的总结. 1. Vision ...
- 10 个免费的Bootstrap Admin 主题,模板收集
In designing websites today, one of the must have frameworks is the twitter bootstrap. To those who ...
- 让你的照片更鲜艳------hsv拉伸
如果你的照片看上去灰蒙蒙的,缺少生机,那么hsv拉伸也许可以帮你的忙.hsv拉伸是一种可以提高图像鲜艳程度的图像增强方法,它能够让图像的颜色更加鲜活.艳丽,而且它的处理结果看上去很自然,比如源图中较暗 ...
- 量子力学与广义相对论的统一——用广义相对论解释海森堡测不准原理 Unification of Quantum Mechanics and General Relativity: Explaining Heisenberg Uncertainty Principle with General Relativity
从海森堡测不准原理的实验开始: 从实验中可以看到,当有光源测定路线,且双孔打开的时候,接收板原波谷处变成了波峰. 对此,广义相对论的解释是:此时电子经过双孔后的轨迹发生了变化.双孔周围的空间弯曲度被光 ...
- C#基础之访问修饰符
C#访问修饰符,无时无刻都在使用,这里记录一下,如果写错的地方,欢迎指正. public :公有的,任何代码均可以访问,应用于所有类或成员: internal:内部的,只能在当前程序集中使用,应用于所 ...
- dev gridview自动列宽和单元、行、checkbox选中和多选
#region 自动列宽 for (int I = 0; I < gridView1.Columns.Count; I++) { this.gridView1.BestFitColumns(); ...