关于建设网站的需求分析,电商推广专业术语,大良网站设计价格,推广获客次用到ListBox的时候#xff0c;鼠标悬浮时#xff0c;ListBoxItem的默认样式太丑了#xff0c;设置了ItemTemplate也不管用#xff0c;像这样的#xff1a;经过几次尝试后#xff0c;终于解决了这个问题#xff0c;记录一下#xff0c;以后就不用到处百度找了。。。其… 次用到ListBox的时候鼠标悬浮时ListBoxItem的默认样式太丑了设置了ItemTemplate也不管用像这样的经过几次尝试后终于解决了这个问题记录一下以后就不用到处百度找了。。。其实很简单只需定义一个ItemContainerStyle即可。 Style x:KeyItemContainerStyle1 TargetTypeListBoxItemSetter PropertySnapsToDevicePixels Valuetrue/Setter PropertyFocusVisualStyle Value{x:Null}/Setter PropertyTemplateSetter.ValueControlTemplate TargetTypeListBoxItemBorder NameBorderPadding7 BackgroundTransparent SnapsToDevicePixelsTrueContentPresenter //BorderControlTemplate.TriggersTrigger PropertyIsSelected ValuetrueSetter TargetNameBorder PropertyBackground ValueGreen/Setter PropertyForeground ValueWhite//TriggerTrigger PropertyIsEnabled ValuefalseSetter PropertyForeground ValueLightGray//Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter
/Style
原来只要设置了一下模板就好了。。。刚做出来时我心里一w头***狂奔。。。下面是测试的代码 StackPanel HorizontalAlignmentStretch TextBlock Text{Binding CurrentItem,StringFormat{}选中项{0}} FontSize18/ListBox x:NamelbPersonList SelectedValue{Binding CurrentItem}SelectedValuePathTextItemContainerStyle{StaticResource ItemContainerStyle1}TextBlock TextHello,world/TextBlock Text你好/TextBlock Text你也好//ListBox/StackPanel
设置了绑定的选中项value的路径这样就可以在后台代码中获取选中项了。后台代码public partial class IconFontButtonDemo : Window, INotifyPropertyChanged{private string _currentItem;public event PropertyChangedEventHandler PropertyChanged;public string CurrentItem{get { return _currentItem; }set{_currentItem value;PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentItem)));}}public IconFontButtonDemo(){InitializeComponent();DataContext this;}}
效果是这样的好了就到这里了wpf,踩坑中