当前位置:网站首页>WPF combobox setting options and anti display
WPF combobox setting options and anti display
2022-06-21 22:34:00 【zLulus】
The effect is as follows :
After initialization, the options are inverted according to the data source , Modify according to operation 、 Clear data source 
data structure
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( Get current options )" Click="GetCurrentSelectItem_Click"></Button>
<Button Content="reset select item( Empty 、 Reset )" Click="ResetSelectItem_Click"></Button>
</StackPanel>
Back end
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" });
// set an option , Reflexion
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( There are no options )");
}
private void ResetSelectItem_Click(object sender, RoutedEventArgs e)
{
// Empty
vm.SelectFruit = null;
}
}
Sample code
边栏推荐
- [deeply understand tcapulusdb technology] tmonitor system upgrade
- 利用for循环,分别计算1-100中奇数的和、偶数的和【方法一】
- 刷题笔记(十六)--二叉树:修改与构造
- Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
- Leetcode question brushing: SF Technology Smart logistics Campus Technology Challenge
- FPGA之道——数字系统之间的接口电平标准
- GDB debugging skills (0) getting started with GDB
- FPGA之道——FPGA开发流程之项目方案与FPGA设计方案
- flutter系列之:flutter中的IndexedStack
- 多进程和多线程的概念
猜你喜欢

JMter测试命令【笔记】
![[deeply understand tcapulusdb technology] tcapulusdb import data](/img/a8/04c4c6703a6137d17666f211d7988d.png)
[deeply understand tcapulusdb technology] tcapulusdb import data

About Eureka starting successfully but accessing 404

【深入理解TcaplusDB技术】单据受理之事务执行

【深入理解TcaplusDB技术】TcaplusDB业务数据备份

LeetCode-543-二叉树的直径
![[in depth understanding of tcapulusdb technology] tcapulusdb business data backup](/img/7f/6d42dc96348001dd6ebd724a44a123.png)
[in depth understanding of tcapulusdb technology] tcapulusdb business data backup

Introduction to software architecture

.bmp图片的文件头解析
![[deeply understand tcapulusdb technology] one click installation of tmonitor background](/img/33/7d390732d6e441a4311189b6e49474.png)
[deeply understand tcapulusdb technology] one click installation of tmonitor background
随机推荐
WPF x:ArrayExtension
WPF x:ArrayExtension
GDB debugging skills (0) getting started with GDB
必讀書籍
Use the do while loop to calculate the odd and even sums in 1-100 [method 1]
MySql踩坑记录
UWP 阴影效果
刷题笔记(十六)--二叉树:修改与构造
Pi4j GPIO pin pull-up resistance, pull-down resistance concept
Use the for loop to calculate the odd and even sums in 1-100 [method 1]
Pi4j pin analog bus, several ideas of control transmission and data transmission
小程序与工业互联网能够相辅相成的原因
flutter系列之:flutter中的IndexedStack
项目流程管理工具OmniPlan Pro 4
WPF 数据绑定:数据源Source-目标Target
IP-guard打印管控,防止打印渠道信息泄露
【深入理解TcaplusDB技术】如何实现Tmonitor单机安装
[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor
pi4j针脚模拟总线,进行控制传输和数据传输的几种思路
将字符串按长度截取成数组