原文:Bind Enum to Combobox.SelectedIndex

Do you mean that you want to bind a variable (not a property) to ComboBox.SelectedIndex? If so, I think that the better method to do this is bind a property to ComboBox.SelectedItem. The following example shows how to bind a property to the ComboBox.SelectedItem. If you want to have more functionality from the binding, you should have the property is a dependency property.

Code Snippet

<Window x:Class="LearningSolution.MainWindow"



xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"



xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"



xmlns:system="clr-namespace:System;assembly=mscorlib"



xmlns:local="clr-namespace:LearningSolution"



Name="TheWindow">



<Window.Resources>



<ObjectDataProvider x:Key="EnumValues"



MethodName="GetValues"



ObjectType="{x:Type system:Enum}">



<ObjectDataProvider.MethodParameters>



<x:Type TypeName="local:LevelEnum" />



</ObjectDataProvider.MethodParameters>



</ObjectDataProvider>



<DataTemplate x:Key="EnumValueTemplate">



<TextBlock Text="{Binding}"/>



</DataTemplate>



</Window.Resources>



<StackPanel>



<ComboBox ItemsSource="{Binding Source={StaticResource EnumValues}}"



ItemTemplate="{StaticResource EnumValueTemplate}"



SelectedItem="{Binding ElementName=TheWindow, Path=TheLevel, Mode=OneWayToSource}"/>



<Button Click="Button_Click">Watch</Button>



</StackPanel>



</Window>



public partial class MainWindow : Window



{



public LevelEnum TheLevel { get; set; }



private void Button_Click(object sender, RoutedEventArgs e)



{



MessageBox.Show(this.TheLevel.ToString());



}



}



public enum LevelEnum { None = 0, First = 1, Second = 2, Third = 3 }



 

For more information about binding and dependency property, you could refer to the following links.

http://msdn2.microsoft.com/en-us/library/ms753192.aspx

http://msdn2.microsoft.com/en-us/library/ms750612.aspx

Bind Enum to Combobox.SelectedIndex的更多相关文章

  1. Bind Enum to ListControl

    当使用MVVM时,相信你和我一样经常有这样的需求: 在ViewModel里定义了一个Enum,它必然是对应UI上的一个ListControl作为不同选项. 有一种做法是使用Converter,将Enu ...

  2. Binding Enum to ComboBox

    1.添加MarkupExtension public class EnumToSourceExtension : MarkupExtension { private Type _type; publi ...

  3. Bind Enum To DropDownList ?

    0. 准备测试数据 0.1 : 枚举 using System; using System.Collections.Generic; using System.Linq; using System.W ...

  4. 【EasyUI】combotree和combobox模糊查询

    这里说的模糊查询指在输入框输入,然后自动在下拉框中显示匹配结果,类似Google搜索提示 EasyUI库已经实现了combobox的查询过滤功能,但只能从头匹配,原因是EasyUI库的代码限制: fi ...

  5. C# WinForm 中ComboBox数据绑定的问题 (转)

    来自:http://blog.sina.com.cn/s/blog_5fb9e26301013wga.html C# WinForm 中ComboBox数据绑定的问题 怎样让WinForm中的Comb ...

  6. ComboBox中如何嵌套TreeView控件

      在ComboBox中嵌套TreeView控件,有时候我们在设计界面的时候,由于界面设计的需要,我们需要将TreeView控件嵌套在ComboBox中,因为TreeView控件实在是太占用地方了,要 ...

  7. C# winform combobox默认选中项方法

    https://blog.csdn.net/easyboot/article/details/68062196 可以使用 Combobox.SelectText = “默认选中文本”; 但是如果Com ...

  8. EasyUi模糊匹配搜索框combobox

    现在项目当中很多已经应用了Jquery-easyUi这个界面框架了,所以,学习一点easyUI的常用工具就显得很重要了,现在介绍的就是我在项目中用到的easyUi的模糊匹配组合框combobox. c ...

  9. ListBox和ComboBox绑定数据简单例子

    1. 将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //自定义了Person类(有Name,Age,Heigth等属性) List<Person> per ...

随机推荐

  1. 第一次使用docker for windows 遇到的坑

    原文:第一次使用docker for windows 遇到的坑 1. 目前win10安装docker, 不需要安装其他工具,可直接到官网下载 2. 此版本的docker可同时运行Windows con ...

  2. [RxJS] Use RxJS mergeMap to map and merge high order observables

    Like RxJS switchMap() is a shortcut for map() and switch(), we will see in this lesson how mergeMap( ...

  3. js课程 1-4 js变量的作用域是怎样的

    js课程  1-4   js变量的作用域是怎样的 一.总结 一句话总结:只有在函数内部前面带var的变量为局部变量,局部变量只能在函数体内使用. 1.什么情况下会出现NaN类型的错误,举一例? Num ...

  4. 微信管理系统基于Flask+Vue+Celery+SQLAlchemy+Redis等实现

    https://zhuanlan.zhihu.com/p/28102858 现在绝大多数同学都在使用微信,不过微信有很多限制,比如: 微信聊天记录只保存在本地,换个手机那些内容就找不到了 微信扫码加群 ...

  5. JQuery通过radio,select改变隐藏显示div

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_36092584/article/details/52740681 1)select下拉框控制d ...

  6. Spark修炼之道(基础篇)——Linux大数据开发基础:第三节:用户和组

    本节主要内容 理解用户和组的概念 用户管理 组管理 权限分配 1. 理解用户和组的概念 在第一讲中我们提到.linux是一种多任务.多用户的操作系统,在讲ls -l命令行我们看到例如以下文件具体信息: ...

  7. 12个被滥用的Android应用程序权限

    Android应用程序须要权限才干正常运作,只是网络犯罪分子会将其用在个人私利上.来看看最常被要求的权限以及它们会怎样被滥用. .网络定位功能 这代表什么:同意应用程序通过网络定位(像是基地台或无线网 ...

  8. 【p094】道路游戏

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小新正在玩一个简单的电脑游戏. 游戏中有一条环形马路,马路上有n个机器人工厂,两个相邻机器人工厂之间由 ...

  9. reduce 阶段遍历对象添加到ArrayList中的问题

    起初遍历values时直接把对象添加到集合中,后来输出结果和预期不符,debug时发现添加到集合中的对象的值全部是最后一个对象的值,网上百度了下,发现是reduce阶段对象重用的问题,reduce阶段 ...

  10. 【16.67%】【codeforces 667C】Reberland Linguistics

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...