当前位置: 首页 > news >正文

怎么自己网站搜不到了百度竞价推广账户优化

怎么自己网站搜不到了,百度竞价推广账户优化,网站建设中外链与内链的技巧,品牌网站策划书前言 wpf中ListBox在针对多个选项选择中的应用十分广泛,本文就来讲解非Mvvm和Mvvm模式下如何使用ListBox。 1、非MVVM模式下 xaml代码: ListBoxItem用于存储ListBox的每一项,可以设置ListBoxItem的属性,比如Foreground控制项的…

前言

wpf中ListBox在针对多个选项选择中的应用十分广泛,本文就来讲解非Mvvm和Mvvm模式下如何使用ListBox。

1、非MVVM模式下

xaml代码:
ListBoxItem用于存储ListBox的每一项,可以设置ListBoxItem的属性,比如Foreground控制项的字体颜色;Background来控制项的背景色;为ListBox注册SelectionChanged事件,该事件代码在MainWindow窗体类中。

 <ListBox x:Name="ListBox_test" SelectionChanged="ListBox_SelectionChanged"><ListBoxItem Foreground="White"  Background="Red"   >项目1</ListBoxItem><ListBoxItem>项目2</ListBoxItem><ListBoxItem>项目3</ListBoxItem><ListBoxItem>项目4</ListBoxItem></ListBox >

MainWindow窗体类

 public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new ViewModel();}private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e){var item = ListBox_test.SelectedItem;}}

运行结果:
在这里插入图片描述

2、MVVM模式下

2.1 SelectionMode为Single

xaml代码:
1)mvvm模式下使用绑定来为ListBox添加数据,通过为ItemsSource绑定ViewModel中的Items属性
2)然后为了获取ListBox中用户选择的哪一项,为SelectedItem绑定ViewModel中的divisionType属性
但是由于SelectedItem是object类型,divisionType是枚举类型,无法直接绑定,所以要使用类型转换

<Window.Resources><local:DivisionTypeToString x:Key="DivisionTypeToString"/></Window.Resources><ListBox SelectionMode="Single" ItemsSource="{Binding Items }" Foreground="Red"  SelectedItem="{Binding divisionType, Converter={StaticResource DivisionTypeToString}}"  > </ListBox >

添加类型转换
下面的Convert方法用于将DivisionType转换成字符串,ConvertBack用于将字符串转换成DivisionType

 public  class DivisionTypeToString: IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){DivisionType divisionType = (DivisionType)value;return divisionType.ToString ();}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){DivisionType divisionType  =(DivisionType)Enum.Parse(typeof (DivisionType),value .ToString ());return divisionType;}}

添加 Model类

public enum DivisionType
{分时1,分时2
}

添加ViewModelBase类

 public class ViewModelBase{public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged(string propname){if (PropertyChanged != null){PropertyChanged(this, new PropertyChangedEventArgs(propname));}}}

添加ViewModel类

 public class ViewModel : ViewModelBase{public ViewModel(){Items = new List<string>();Items.Add("分时1");Items.Add("分时2");}private List<string> items;public List<string> Items{get{return items;}set{this.items = value;OnPropertyChanged("Items");}}private DivisionType _divisionType = DivisionType.分时1;public DivisionType divisionType{get{return _divisionType;}set{this._divisionType = value;OnPropertyChanged("divisionType");}}}

运行结果:
在这里插入图片描述

2.2 SelectionMode为Multiple

xaml代码:
1)mvvm模式下使用绑定来为ListBox添加数据,通过为ItemsSource绑定ViewModel中的Items属性
2)又因为是多选,所以不能使用SelectedItem属性的绑定,得使用 SelectionChanged事件,然后在事件中获取SelectedItems属性从而获取所有用户选择的项,所以这里使用使用Interaction.Trigger,EventName指定绑定的事件名称,CommandSelectionChanged是绑定的ViewModel中的命令。(这里要注意使用Interaction.Triggers需要引用System.Windows.Interactivity.dll)

<Window.Resources><local:DivisionTypeToString x:Key="DivisionTypeToString"/></Window.Resources><ListBox x:Name="listBox_test2" SelectionMode="Multiple" ItemsSource="{Binding Items }" Foreground="Red"     ><Event:Interaction.Triggers><Event:EventTrigger EventName="SelectionChanged"><Event:InvokeCommandAction Command="{Binding CommandSelectionChanged}" CommandParameter="{Binding ElementName=listBox_test2}"/></Event:EventTrigger></Event:Interaction.Triggers></ListBox >

添加DelegateCommand

 public class DelegateCommand : ICommand{private readonly Action<object> _execute;private readonly Func<object, bool> _canExecute;public DelegateCommand(Action<object> execute, Func<object, bool> canExecute = null){_execute = execute ?? throw new ArgumentNullException(nameof(execute));_canExecute = canExecute;}public bool CanExecute(object parameter){if (_canExecute != null){return _canExecute(parameter);}else{return true;}}public void Execute(object parameter){_execute(parameter);}public event EventHandler CanExecuteChanged{add => CommandManager.RequerySuggested += value;remove => CommandManager.RequerySuggested -= value;}public void RaiseCanExecuteChanged(){CommandManager.InvalidateRequerySuggested();}}

添加 Model类

public enum DivisionType
{分时1,分时2
}

添加ViewModelBase类

 public class ViewModelBase{public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged(string propname){if (PropertyChanged != null){PropertyChanged(this, new PropertyChangedEventArgs(propname));}}}

添加ViewModel类

 public class ViewModel : ViewModelBase{public ICommand CommandSelectionChanged { get; set; }public ViewModel(){Items = new List<string>();Items.Add("分时1");Items.Add("分时2");CommandSelectionChanged = new DelegateCommand(ListBoxSelectionChanged);}private List<string> items;public List<string> Items{get{return items;}set{this.items = value;OnPropertyChanged("Items");}}private ObservableCollection<DivisionType> _divisionTypeList = new ObservableCollection<DivisionType>();public ObservableCollection<DivisionType> divisionTypeList{get{return _divisionTypeList;}set{this._divisionTypeList = value;OnPropertyChanged("divisionTypeList");}}private void ListBoxSelectionChanged(object obj){divisionTypeList.Clear();ListBox listBox =(ListBox) obj;foreach ( var item in listBox.SelectedItems){DivisionType divisionType=(DivisionType) Enum.Parse(typeof (DivisionType),item.ToString ());divisionTypeList.Add(divisionType);}}}

运行结果:
在这里插入图片描述

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》
2、《C#与PLC通信从入门到精通 》
3、《C# Modbus通信从入门到精通》
4、《C#Socket通信从入门到精通 》
5、《C# MES通信从入门到精通》
6、《winform控件从入门到精通》
7、《C#操作MySql数据库从入门到精通》

http://www.dtcms.com/a/448895.html

相关文章:

  • 免费建立网站论坛北仑网站建设
  • 手机如何网站wordpress添加新页面跳转
  • 做装修效果图的网站wordpress常见的15个问题
  • 天猫网站建设的目的wordpress获取当前分类下的子分类
  • 网站程序预装wordpress echo
  • 化妆品网站的搭建网站二级页面需不需要设置关键词
  • 资源下载网站wordpress学工网站建设
  • 附近网站建设服务公司网站建设公司怎么投诉
  • 网站建设預算甘肃省建设银行校园招聘网站
  • 关于seo网站优化公司阿里巴巴电脑版网页
  • 珠海市住房和城乡建设厅网站跨境电商怎么做流程
  • 万由nas做网站网站设计为什么要域名
  • 企业门户网站什么意思怎么改网站关键词
  • 温州建设局网站首页最有效的网站推广设计
  • 各种免费源码共享网站网站建设哪些职位
  • 网站怎么做才会有收录拓者设计吧账号
  • 月子中心网站设计2345网址导航站
  • seo综合查询站长工具关键词搬家网站建设案例说明
  • 网站的策划做推广上海人才网最新招聘2021
  • 快手点赞购买网站网站建设主要内容包括
  • 手机网站建设一般要多少钱程序员培训比较好机构有哪些
  • 团购网站优化电商自建站
  • 标准型网站建设广州网站建设培训学校
  • 网站建设制作视频厦门装修公司排名前十口碑推荐
  • 网站建设的活怎么接互联网推广服务
  • wordpress页面发布失败为什么seo顾问阿亮博客
  • 制作网站什么制作软件北京动漫设计公司有哪些
  • 广州一网通注册公司手机优化如何弄到100
  • 济南网站建设培训班那个网站能找到人
  • 网站前台显示数据库指定分类怎么做phpdw2020网页设计教程