赞
踩
定义枚举值:
- public enum Subjects
- {
- 语文,
- 数学,
- 英语,
- 体育
- }
方式一:
xaml文件:需要引入xmlns:core="clr-namespace:System;assembly=mscorlib";其中枚举我是写在ViewModel中的,所以还需要引入xmlns:viewModel="clr-namespace:WriteTXT.ViewModels"
- <Window.Resources>
- <ObjectDataProvider x:Key="Subjects" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
- <ObjectDataProvider.MethodParameters>
- <x:Type Type="viewModel:Subjects"/>
- </ObjectDataProvider.MethodParameters>
- </ObjectDataProvider>
- </Window.Resources>
- <ComboBox ItemsSource="{Binding Source={StaticResource Subjects}}" SelectedItem="{Binding Subject1}"/>
- <Label Grid.Column="1" Content="{Binding Sub1}"/>
ViewModel代码:
- private Subjects _subject1;
-
- public Subjects Subject1
- {
- get { return _subject1; }
- set
- {
- _subject1 = value;
- Sub1 = value.ToString();
- this.RaisePropertyChanged("Subject1");
- }
- }
-
- private string _sub1 = "初始值";
-
- public string Sub1
- {
- get { return _sub1; }
- set
- {
- _sub1 = value;
- this.RaisePropertyChanged("Sub1");
- }
- }
方式二:
xaml文件:是SelectedValue而不是SelectedItem
- <ComboBox ItemsSource="{Binding Subs}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding Subject1}"/>
- <Label Grid.Column="1" Content="{Binding Sub1}"/>
ViewModel代码:
- private Dictionary<Subjects, string> _subs;
-
- public Dictionary<Subjects, string> Subs
- {
- get { return _subs; }
- set
- {
- _subs = value;
- this.RaisePropertyChanged("Subs");
- }
- }
-
- public MainWindowViewModel()
- {
- Subs = new Dictionary<Subjects, string>();
- foreach (Subjects em in Enum.GetValues(typeof(Subjects)))
- Subs[em] = "学科:" + em.ToString();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。