好的,所以我在这里找到了很多关于这个的问题,但是对我来说这些问题看起来都很复杂。
我有一个绑定到viewmodel类的组合框。
class SettingsViewModel
{
public PictureRecognitionIntensivity PictureRecognitionIntensivity { get; set; }
public Array PictureRecognitionIntensivityValues
{
get { return PictureRecognitionIntensivity.GetValues(typeof(PictureRecognitionIntensivity)); }
}
public SettingsViewModel()
{
// Set default values for testing;
this.PictureRecognitionIntensivity = PictureRecognitionIntensivity.Moderate;
}
}
XAML部分:
<ComboBox SelectedItem="{Binding Path=PictureRecognitionIntensivity}" ItemsSource="{Binding Path=GetPictureRecognitionIntensivityValues}" />
此代码正确地将值加载到组合框中,并且当选择更改时,值将更改为所选项目,以使其正常工作,除非它没有将初始状态加载到组合框中。 (开头是空的。)
在某个地方,我看到Enum.GetValues返回一个字符串,所以我尝试将它转换为字符串,如下所示:
return this.PictureRecognitionIntensivity.toString();
但它也没有用。我的问题是什么是简单易用的方式(我不能相信它在2016年仍然如此复杂,我必须编写20行代码。)将枚举实例添加到组合框并设置selectedItem为了它。
答案 0 :(得分:2)
我建议给定枚举的最简单方法
public enum PictureRecognitionIntensivity
{
FIRST,
SECOND,
THIRD,
FORTH,
}
在视图资源中声明静态实例,如下所示
<Window.Resources>
<ObjectDataProvider x:Key="dataFromEnum" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:PictureRecognitionIntensivity"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
然后在你的ComboBox中按如下方式绑定它:
<ComboBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}" SelectedItem="{Binding intensity }"/>
然后设置初始枚举值应该按预期工作。
答案 1 :(得分:0)
GetValues返回一个整数数组,因为枚举可以转换为数字,您必须将它们转换为字符串,然后将您的属性更改为字符串(在内部,您将存储枚举值)。
class SettingsViewModel
{
PictureRecognitionIntensivity intensity;
public string Intensivity
{
get
{
return intensity.ToString();
}
set
{
intensity = (PictureRecognitionIntensivity)Enum.Parse(typeof(PictureRecognitionIntensivity), value);
}
}
public string[] PictureRecognitionIntensivityValues
{
get {
List<string> values = new List<string>();
Array iValues = PictureRecognitionIntensivity.GetValues(typeof(PictureRecognitionIntensivity));
foreach(int value in iValues)
values.Add(((PictureRecognitionIntensivity)value).ToString());
return values.ToArray();
}
}
public SettingsViewModel()
{
// Set default values for testing;
this.intensity = PictureRecognitionIntensivity.Moderate;
}
}
答案 2 :(得分:0)
我认为您尚未实施INotifyPropertyChanged
。
请参阅此示例代码:
Window1.xaml
<Grid>
<ComboBox x:Name="cmbChessTitles" SelectedItem="{Binding ChosenTitle, Mode=TwoWay}" FontSize="25" Width="200" Height="45"></ComboBox>
<TextBlock HorizontalAlignment="Left" Margin="246,121,0,0" Text="{Binding ChosenTitle}" TextWrapping="Wrap" VerticalAlignment="Top" Height="39" Width="200" Background="#FFF01313"/>
</Grid>
Window1.xaml.cs
public partial class Window1 : Window
{
public ViewModel vm = new ViewModel();
public Window1()
{
InitializeComponent();
this.DataContext = vm;
cmbChessTitles.ItemsSource = vm.Titles;
vm.ChosenTitle = ChessTitles.WorldChampion;
cmbChessTitles.SelectedItem = vm.ChosenTitle;
}
}
public class ViewModel:INotifyPropertyChanged
{
public Array Titles
{
get { return Enum.GetValues(typeof(ChessTitles)); }
}
private ChessTitles _chosenTitle;
public ChessTitles ChosenTitle
{
get{ return _chosenTitle ;}
set { _chosenTitle = value; OnPropertyChanged("ChosenTitle"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string prop)
{
if(PropertyChanged !=null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
public enum ChessTitles
{
WorldChampion,
GrandMaster,
FideMaster,
InternationalMaster
}
答案 3 :(得分:0)
也许可以帮助设置SelectedValue而不是SelectedItem:
array.map(item => ({...item, details: [item.details.find(detailsItem => detailsItem.day === "Monday")]}));