(WPF) ComboBox 之绑定
1. 在UI(Xaml) 里面直接绑定数据.
- <Window x:Class="WpfTutorialSamples.ComboBox_control.ComboBoxSample"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="ComboBoxSample" Height="150" Width="200">
- <StackPanel Margin="10">
- <ComboBox>
- <ComboBoxItem>ComboBox Item #1</ComboBoxItem>
- <ComboBoxItem IsSelected="True">ComboBox Item #2</ComboBoxItem>
- <ComboBoxItem>ComboBox Item #3</ComboBoxItem>
- </ComboBox>
- </StackPanel>
- </Window>
效果如下:
2. 动态绑定数据.
2.1 绑定XML到ComboBox
前台Xaml里面需要在Resource里面指定XmlDataProvider. 并设定绑定. 后台无需任何代码。
- <Window x:Class="WpfApplication1.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="350" Width="525">
- <Window.Resources>
- <XmlDataProvider x:Key="DataProvider" Source="pack://siteoforigin:,,,/TestData.xml" XPath="Countries"/>
- </Window.Resources>
- <StackPanel>
- <ComboBox x:Name="com_Country" ItemsSource="{Binding Source={StaticResource DataProvider}, XPath=Country}" DisplayMemberPath="@Name" Margin="5"/>
- <ComboBox x:Name="com_City" DataContext="{Binding ElementName=com_Country, Path=SelectedItem}" ItemsSource="{Binding XPath=City}" DisplayMemberPath="@Name" Margin="5"/>
- <TextBlock Margin="5">
- <TextBlock.Inlines>
- <Run Text="Selected Country: "/>
- <Run Text="{Binding ElementName=com_Country, Path=SelectedItem.Attributes[Name].Value, Mode=OneWay}"/>
- <LineBreak/>
- <Run Text="Selected City: "/>
- <Run Text="{Binding ElementName=com_City, Path=SelectedItem.Attributes[Name].Value}"/>
- </TextBlock.Inlines>
- </TextBlock>
- </StackPanel>
- </Window>
3. 绑定Dictionary到ComboBox.
- private void BindingToProjects()
- {
- this.projects.Add("AAA", new List<string>()
- {
- "VID_045",
- "VID_046"
- this.projects.Add("BBB", new List<string>()
- {
- "VID_111",
- "VID_222",
- });
- this.cbProjectName.ItemsSource = this.projects;
- this.cbProjectName.DisplayMemberPath = "Key";
- this.cbProjectName.SelectedValuePath = "Key";
- }
- private void cbProjectName_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- this.lbVidPid.Items.Clear();
- List<string> registryList = this.projects.Where(dic => dic.Key == this.cbProjectName.SelectedValue.ToString()).Select(dic => dic.Value).First();
- foreach (var registryKey in registryList)
- {
- this.usbRegistryList.Add(registryKey);
- this.lbVidPid.Items.Add(registryKey);
- }
- }
参考: http://www.wpf-tutorial.com/list-controls/combobox-control/
(WPF) ComboBox 之绑定的更多相关文章
- WPF dataGrid下的ComboBox的绑定
WPF dataGrid下的ComboBox的绑定 Wpf中dataGrid中的某列是comboBox解决这个问题费了不少时间,不废话了直接上代码 xaml 代码 <DataGridTempla ...
- WPF 支持集合绑定的控件
WPF 支持集合绑定的控件 ListBox ComboBox ListView DataGrid
- [WinForm] DataGridView 绑定 DT && ComboBox 列绑定 Dict
一 需求介绍 一般像枚举类型的数据,我们在数据库里存储着诸如(1.2.3.4-)或者("001"."002"."003"-)此类,但是界面 ...
- WPF数据双向绑定
设置双向绑定,首先控件要绑定的对象要先继承一个接口: INotifyPropertyChanged 然后对应被绑定的属性增加代码如下: 意思就是当Age这个属性变化时,要通知监听它变化的人. 即:Pr ...
- WPF的DataTrigger绑定自身属性
原文:WPF的DataTrigger绑定自身属性 <DataTrigger Binding="{Binding RelativeSource={RelativeSource self} ...
- WPF UserControl 的绑定事件、属性、附加属性
原文:WPF UserControl 的绑定事件.属性.附加属性 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Vblegend_2013/arti ...
- 封装:WPF中可以绑定的BindPassWord控件
原文:封装:WPF中可以绑定的BindPassWord控件 一.目的:本身自带的PassWord不支持绑定 二.Xaml部分 <UserControl x:Class="HeBianG ...
- WPF ComboBox(转)
WPF ComboBox 创建一个ComboBox控件,并设置ComboBox控件的名称,高度,宽度.及设置ComboBox的垂直和水平对齐. <ComboBox Name="Comb ...
- 继续聊WPF——获取ComboBox中绑定的值
千万不要认为WPF中的数据绑定会很复杂,尽管它的确比Winform程序灵活多了,但其本质是不变的,特别是ComboBox控件,我们知道在Winform中对该控件的有两个专为数据绑定而设定的属性——Di ...
随机推荐
- Bridge Across Islands POJ - 3608 旋转卡壳求凸包最近距离
\(\color{#0066ff}{题目描述}\) 几千年前,有一个小王国位于太平洋的中部.王国的领土由两个分离的岛屿组成.由于洋流的冲击,两个岛屿的形状都变成了凸多边形.王国的国王想建立一座桥来连接 ...
- 线段树 SP2713 GSS4 - Can you answer these queries IV暨 【洛谷P4145】 上帝造题的七分钟2 / 花神游历各国
SP2713 GSS4 - Can you answer these queries IV 「题意」: n 个数,每个数在\(10^{18}\) 范围内. 现在有「两种」操作 0 x y把区间\([x ...
- window.performance
利用window.performance查看网页性能 一般我们可以通过浏览器的调试工具-网络面板,或者代理工具查看网页加载过程中的各个阶段的耗时.而利用window.performance属性则可以获 ...
- kuangbin专题十六 KMP&&扩展KMP HDU1358 Period
For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...
- sqlmap用法
用法 Usage: python sqlmap.py [options] Options: -h, --help Show basic help message and exit -hh Show a ...
- Qt 学习之路 2(4):信号槽
Home / Qt 学习之路 2 / Qt 学习之路 2(4):信号槽 Qt 学习之路 2(4):信号槽 豆子 2012年8月23日 Qt 学习之路 2 110条评论 信号槽是 Qt 框架引以 ...
- SprimgMVC学习笔记(一)—— SpringMVC入门
一.什么是 SpringMVC ? 在介绍什么是 SpringMVC 之前,我们先看看 Spring 的基本架构.如下图: 我们可以看到,在 Spring 的基本架构中,红色圈起来的 Spring W ...
- js 返回 差 集
//接受两个数组,返回差集 function getDiffSet(a,b){ a.sort(charSort); b.sort(charSort); if(a.length>=b.length ...
- 华东交通大学2015年ACM“双基”程序设计竞赛1004
Problem D Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- MetricStatTimer
package org.apache.storm.metric.internal; import java.util.Timer; /** * Just holds a singleton metri ...