原文: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++竞赛 C】yyy的数学公式

    时间限制:1s 内存限制:32MB 问题描述 yyy遇到了一个数学问题如下: S_n=∑F(i) 其中F(i)表示i的最大奇因数 yyy的数学非常好,很快就得到了结果.现在他把问题交给你,你能解决吗? ...

  2. JNI_Android 项目中调用.so动态库实现详解

    转自:http://www.yxkfw.com/?p=7223 1. 在Eclipse中创建项目:TestJNI 2. 新创建一个class:TestJNI.java package com.wwj. ...

  3. SimpleDateFormat.format的简单使用小结

    format的用法 是将当前时间格式转换为指定格式 场景一:给定毫秒数或者当前系统时间,返回指定时间格式 输入         Date date=new Date();//获得系统当前的时间 //  ...

  4. 动态创建Fragment

    在android3.0之前.每创建一个界面就要新创建一个activity. 在3.0之后引入了Fragment.相当于一个轻量级的activity.不须要在清单文件配置. 先来看下怎样创建和使用Fra ...

  5. try catch finally中的return

    try catch 中finally语句总是可以执行的,不管try中是否含有return语句 public class TestReturn { public static void main(Str ...

  6. 用C语言编写简单的病毒

    [摘要]在分析病毒机理的基础上,用C语言写了一个小病毒作为实例,用TURBOC2.0实现. [Abstract] This paper introduce the charateristic of t ...

  7. ANDROID L——RecyclerView,CardView进口和使用(Demo)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 这篇文章是ANDROID L--Material Design具体解释(UI控 ...

  8. C++技术问题总结-第0篇 类型转换

    从今天開始,对C++经常使用技术做个总结. 參考书籍:<C++Primer>.<C++对象模型>.<设计模式>.<Windows核心编程>.<ST ...

  9. C++闭包,一样很简单

    引用百度上对闭包的定义:闭包是指可以包含自由(未绑定到特定对象)变量的代码块:这些变量不是在这个代码块内或者任何全局上下文中定义的,而是在定义代码块的环境中定义(局部变量).“闭包” 一词来源于以下两 ...

  10. Struts2——(3)ValueStack(值栈)

    一.ValueStack 主要用于存储请求相关信息,内部结构如下 root区:被称为根存储区,是一个栈结构,栈顶元素为当前请求的Action对象. context区:被称为变量存储区,是一个Map结构 ...