原文: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. POST提交数据时四种常见的数据格式

    最近项目部署到新环境tomcat+mysql,想看看项目部署成功没有,就用soupui调对应接口开测试,soupui使用比较简单,给上接口地址,入参xml报文,把入参的media Type设置为app ...

  2. Annotation研究的一些学习资料

    转自chanyinhelv原文Annotation研究的一些学习资料 下面是我最近对Annotation研究的一些学习资料,收集于此,供大家学习之用. 一.Annotation要素类介绍 在GeoDa ...

  3. iptables 重启系统生效

    1. 重启系统生效 开启: chkconfig iptables on 关闭: chkconfig iptables off   2. 即时生效,重启后失效 开启: service iptables ...

  4. #781 – 多个变换执行的顺序问题(Transform Order Matters)

    原文:#781 – 多个变换执行的顺序问题(Transform Order Matters) 原文地址:  https://wpf.2000things.com/2013/03/21/781-tr ...

  5. Java8内存模型

    一.JVM内存模型 内存空间(Runtime Data Area)中可以按照是否线程共享分为两块,线程共享的是方法区(Method Area)和堆(Heap),线程独享的是Java虚拟机栈(Java ...

  6. 【水水水】【洛谷 U4566】赛车比赛

    题目背景 kkk在赛车~ 题目描述 现在有N辆赛车行驶在一条直线跑道(你可以认为跑道无限长)上.它们各自以某种速度匀速前进,如果有两辆车A车和B车,A车在B车的后面,且A车的速度大于B车的速度,那么经 ...

  7. 打造开发React Native的Sublime

     之前一年多一直用Sublime Text做前端开发,最近做React开发,发现不太好用,就尝试其他编辑器.WebStorm和VS Code都用过,WebStorm基本不用装插件,可以直接用,但用习惯 ...

  8. 在web应用程序中在网页中对Web.config文件进行数据连接配置

    using System; using System.Collections.Generic; using System.Configuration; using System.Web.Configu ...

  9. Js之全局函数

    全局函数 encodeURI()decodeURI()encodeURIComponent()decodeURIComponent()escape()unescape()eval() String() ...

  10. tomcat添加跨域访问功能-Access-Control-Allow-Origin:*

    目标:tomcat服务器提供的接口,不能在其他域中访问的时候,需要增 Access-Control-Allow-Origin:* 直接配置的方法很多,但是我一个没有成功过,所以只能自己写拦截器了. 1 ...