Silverlight中关于ComboBox的各种使用(基本上是汇总了往上的一些方法)

2023-10-28 06:08

本文主要是介绍Silverlight中关于ComboBox的各种使用(基本上是汇总了往上的一些方法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前端放置了几个ComboBox的控件。

复制代码
<Grid x:Name="LayoutRoot" Background="White">
<ComboBox Height="23" HorizontalAlignment="Left" Margin="26,49,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="223,49,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox2_SelectionChanged" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="26,140,0,0" Name="comboBox3" VerticalAlignment="Top" Width="120" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="223,140,0,0" Name="comboBox4" VerticalAlignment="Top" Width="120" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="26,199,0,0" Name="comboBox5" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox5_SelectionChanged" />
</Grid>
复制代码


后端的Cs文件如下:

复制代码
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
InitlizePage();
}
public void InitlizePage()
{
#region comboBox1 最基本的绑定
ComboBoxItem lbi = new ComboBoxItem();
lbi.SetValue(ComboBoxItem.ContentProperty, "江苏");
this.comboBox1.Items.Add("武汉");
this.comboBox1.Items.Add("大连");
this.comboBox1.Items.Add("苏州");
this.comboBox1.Items.Add(lbi);
this.comboBox1.SelectedItem = lbi;
#endregion
#region 构建了一个数据源
List<Product> _list = new List<Product>();
_list.Add(new Product() { ID = 11, Name = "产品1" });
_list.Add(new Product() { ID = 22, Name = "产品2" });
_list.Add(new Product() { ID = 33, Name = "产品3" });
_list.Add(new Product() { ID = 44, Name = "产品4" });
_list.Add(new Product() { ID = 55, Name = "产品5" });
_list.Add(new Product() { ID = 66, Name = "产品6" });
#endregion
#region 添加数据源
this.comboBox2.DisplayMemberPath = "Name";
//this.comboBox2.SelectedValuePath = "ID";//没有指定Value值
this.comboBox2.UpdateLayout();
this.comboBox2.ItemsSource = _list;
//进行初始化赋值,使用SelectedItem  ComboBox的数据初始化跟web中的DropDownList是不一样的
this.comboBox2.SelectedItem = (from p in this.comboBox2.Items
where (p as Product).Name == "产品4"
select p).First();
#endregion
#region 
this.comboBox5.DisplayMemberPath = "Name";
this.comboBox5.SelectedValuePath = "ID";//指定Value值
this.comboBox5.UpdateLayout();
this.comboBox5.ItemsSource = _list;
int SelectedIndex = -1;
for(int i =0;i<_list.Count;i++ )
{
if (_list[i].ID == 33)
{
SelectedIndex = i;
break;
}
}
//通过SelectedIndex来进行初始化绑定
this.comboBox5.SelectedIndex = SelectedIndex;
#endregion 
#region 添加自定义Item
ComboBoxItem cbiRight = new ComboBoxItem();
cbiRight.Background = new SolidColorBrush(Colors.Yellow);
cbiRight.HorizontalContentAlignment = HorizontalAlignment.Right;
cbiRight.SetValue(ComboBoxItem.ContentProperty, "上海");
ComboBoxItem cbiCenter = new ComboBoxItem(); 
cbiCenter.Background = new SolidColorBrush(Colors.Cyan);
cbiCenter.HorizontalContentAlignment = HorizontalAlignment.Center; 
cbiCenter.SetValue(ComboBoxItem.ContentProperty, "北京");
ComboBoxItem cbiLeft = new ComboBoxItem();
cbiLeft.Background = new SolidColorBrush(Colors.LightGray); 
cbiLeft.HorizontalContentAlignment = HorizontalAlignment.Left;
cbiLeft.SetValue(ComboBoxItem.ContentProperty, "深圳");
this.comboBox3.Items.Add(cbiRight);
this.comboBox3.Items.Add(cbiCenter);
this.comboBox3.Items.Add(cbiLeft);
#endregion
Image img = new Image(); 
img.Source = new BitmapImage(new Uri("img/1.png", UriKind.Relative));
Label lbl = new Label();
lbl.Content = "带图片的选项";
StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Horizontal; 
sp.Children.Add(img);
sp.Children.Add(lbl);
ComboBoxItem multipleCmb = new ComboBoxItem(); 
multipleCmb.Content = sp;
this.comboBox4.Items.Add(multipleCmb);  
}
private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Product product = (sender as ComboBox).SelectedItem as Product;
int selectID = product.ID;
string selectedName = product.Name;
//MessageBox.Show(selectID.ToString() + selectedName);

Product product2 = comboBox2.SelectedValue as Product;
int selectedID2 = product2.ID;
string selectedName2 = product2.Name;
//MessageBox.Show(selectedID2.ToString() + selectedName2);
        }
//
        private void comboBox5_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//MessageBox.Show(((ComboBox)sender).SelectedValue.ToString());//获取value
//MessageBox.Show(((Product)((ComboBox)sender).SelectedItem).Name);//获取name呢????
        }
}
复制代码

当然,其中我们还要构建一个Product的类

public class Product
{
public int ID { get; set; }
public string Name { get; set; }
}

这篇关于Silverlight中关于ComboBox的各种使用(基本上是汇总了往上的一些方法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/291213

相关文章

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

Nginx安全防护的多种方法

《Nginx安全防护的多种方法》在生产环境中,需要隐藏Nginx的版本号,以避免泄漏Nginx的版本,使攻击者不能针对特定版本进行攻击,下面就来介绍一下Nginx安全防护的方法,感兴趣的可以了解一下... 目录核心安全配置1.编译安装 Nginx2.隐藏版本号3.限制危险请求方法4.请求限制(CC攻击防御)

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断