当前位置:网站首页>WPF ComboBox设置选项与反显
WPF ComboBox设置选项与反显
2022-06-21 20:41:00 【zLulus】
效果如下:
初始化后根据数据源反显选项,根据操作修改、清空数据源
数据结构
internal class SettingWithComboBoxDemoViewModel : INotifyPropertyChanged
{
public ObservableCollection<FruitViewModel> Fruits { get; set; }
private FruitViewModel selectFruit;
public FruitViewModel SelectFruit
{
get { return selectFruit; }
set
{
if (selectFruit != value)
{
selectFruit = value;
NotifyPropertyChanged(nameof(SelectFruit));
}
}
}
#region INotifyPropertyChanged Members
/// <summary>
/// Need to implement this interface in order to get data binding
/// to work properly.
/// </summary>
/// <param name="propertyName"></param>
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
ui
<StackPanel Orientation="Vertical">
<ComboBox ItemsSource="{Binding Fruits}" DisplayMemberPath="Name" SelectedItem="{Binding SelectFruit,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></ComboBox>
<Button Content="get current select item(获得当前选项)" Click="GetCurrentSelectItem_Click"></Button>
<Button Content="reset select item(清空、重置)" Click="ResetSelectItem_Click"></Button>
</StackPanel>
后端
public partial class SettingWithComboBoxDemo : UserControl
{
SettingWithComboBoxDemoViewModel vm { get; set; }
public SettingWithComboBoxDemo()
{
InitializeComponent();
vm = new SettingWithComboBoxDemoViewModel();
vm.Fruits = new ObservableCollection<FruitViewModel>();
vm.Fruits.Add(new FruitViewModel() { Id = 1, Name = "Apple" });
vm.Fruits.Add(new FruitViewModel() { Id = 2, Name = "Pear" });
vm.Fruits.Add(new FruitViewModel() { Id = 3, Name = "Banana" });
//设置选项,反显
vm.SelectFruit = vm.Fruits.FirstOrDefault(x => x.Id == 1);
DataContext = vm;
}
private void GetCurrentSelectItem_Click(object sender, System.Windows.RoutedEventArgs e)
{
if(vm.SelectFruit!=null)
MessageBox.Show($"{vm.SelectFruit.Name}");
else
MessageBox.Show($"None(没有选项)");
}
private void ResetSelectItem_Click(object sender, RoutedEventArgs e)
{
//清空
vm.SelectFruit = null;
}
}
示例代码
边栏推荐
- How to uninstall a package installed with the CONDA command
- [deeply understand tcapulusdb technology] tmonitor module architecture
- 001 new construction project based on opencvsharp
- promise错误捕获处理——Promisifying技术
- 班主任让开股票账户,在启牛开户安全吗?
- Use the for loop to calculate the odd and even sums in 1-100 [method 2]
- Learn vector -- how to use common interfaces
- 将字符串按长度截取成数组
- 利用do while循环,分别计算1-100中奇数的和、偶数的和【方法一】
- 迅为iTOP-3568开发板安装 RKNN Toolkit Lite2
猜你喜欢

Nacos installation guide

.bmp图片的文件头解析
![[deeply understand tcapulusdb technology] transaction execution of document acceptance](/img/7c/25a88f46e02cebd2e003b9590b9c13.png)
[deeply understand tcapulusdb technology] transaction execution of document acceptance

nuxt ssr打包和部署

翻译软件Bob安装教程

左手代码,右手开源,开源路上的一份子

关于eureka启动成功但是访问404问题

【深入理解TcaplusDB技术】 Tmonitor模块架构
![[in depth understanding of tcapulusdb technology] tcapulusdb construction data](/img/64/4d7ec393d8469cdadc89078a8cf4b1.png)
[in depth understanding of tcapulusdb technology] tcapulusdb construction data

About Eureka starting successfully but accessing 404
随机推荐
GDB调试实战(8)给程序传递启动参数
Correspondence between rtx3090 and pytorch versions
翻译软件Bob安装教程
利用while循环,分别计算1-100中奇数的和、偶数的和【方法一】
Nacos安装指南
软件架构介绍
Introduction to software architecture
[deeply understand tcapulusdb technology] one click installation of tmonitor background
Leetcode question brushing: SF Technology Smart logistics Campus Technology Challenge
Instadeep ltd:arthur flajolet | group based rapid reinforcement learning on a single machine
Second understanding microservice
Use the while loop to calculate the odd and even sums in 1-100 [method 2]
利用do while循环,分别计算1-100中奇数的和、偶数的和【方法一】
Pal2nal| how to run pal2nal from the command line
es7创建索引模板
可乐与凉茶加速互卷
LeetCode-543-二叉树的直径
班主任让开股票账户,在启牛开户安全吗?
高项-立项管理
UEFI dual system + dual hard disk installation