原文: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. C#验证手机号

    using System.Text.RegularExpressions; private bool IsMobile(string phoneNo) { return Regex.IsMatch(p ...

  2. Qt on Android: http下载与Json解析

    百度提供有查询 ip 归属地的开放接口,当你在搜索框中输入一个 ip 地址进行搜索,就会打开由 ip138 提供的百度框应用,你能够在框内直接输入 ip 地址查询.我查看了页面请求,提取出查询 ip ...

  3. Android 升级到Android Studio2.2 后打不开以前版本的项目

    1.找到 build.gradle 用记事本打开,修改如下: // Top-level build file where you can add configuration options commo ...

  4. Yii学习笔记之四(表单验证 api 翻译)

    1.表单验证 对于用户输入的全部数据,你不能信任,必须加以验证. 全部框架如此.对于yii 能够使用函数  yii\base\Model::validate()  进行验证 他会返回boolean值的 ...

  5. 接入Erlang控制台的几种方法

    在window中调试的时候我们可以通过启动多个cmd窗口运行Erlang节点,在生产环境中我们需要Erlang服务在Centos服务器上后台运行;这就需要在启动的时候添加启动参数detached来脱离 ...

  6. Ultra-wideband (UWB) secure wireless device pairing and associated systems

    Methods and systems are disclosed for ultra-wideband (UWB) secure wireless device pairing. Secure pa ...

  7. matlab 图像的保存

    gcf:获取当前显示图像的句柄: 默认 plot 的 position 是 [232 246 560 420] 0. save >> A = randn(3, 4); >> B ...

  8. HSQL一个简短的引论

    前言     在对dao层写測试类的时候,我们须要一个測试数据库,一般我们会是专门建立一个真实的測试数据库,可是有了HSQLDB事情就变得简单了起来. 正题 一.简单介绍: hsql数据库是一款纯Ja ...

  9. GIT 单个文件还原到历史版本号

    首先检查在历史文件中的版本号信息:git log <file> 将文件还原到历史版本号:git reset 版本号 <file> 文档工作区的检测:git checkout - ...

  10. New in 10.2.2: C++ and Debugger Improvements

    In RAD Studio 10.2.2, we've made a number of great quality improvements for the C++ toolchain and fo ...