当前位置:网站首页>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;
}
}
示例代码
边栏推荐
- Beijing accelerates ecological construction, Medtronic Internet and Moore thread complete product compatibility and mutual certification
- vb屏幕分辨率设置和获取_hawkol_新浪博客
- Notes on topic brushing (16) -- binary tree: modification and construction
- [deeply understand tcapulusdb technology] one click installation of tmonitor background
- Pal2nal| how to run pal2nal from the command line
- 采样器合集
- window安装scoop的国内镜像
- 【深入理解TcaplusDB技术】TcaplusDB构造数据
- Pi4j GPIO pin pull-up resistance, pull-down resistance concept
- nuxt ssr打包和部署
猜你喜欢

刷题笔记(十六)--二叉树:修改与构造

Summary of Li Kou brush questions 4 (MySQL version)

UEFI dual system + dual hard disk installation

LeetCode-543-二叉树的直径

University of Virginia: ingy Elsayed aly | logic based reward formation in Multi-Agent Reinforcement Learning

技术分享 | kubernetes pod 简介

Leetcode question brushing: SF Technology Smart logistics Campus Technology Challenge

【深入理解TcaplusDB技术】Tmonitor后台一键安装

flutter系列之:flutter中的IndexedStack

Contact five heart matchmaker to take off the order
随机推荐
Instadeep ltd:arthur flajolet | group based rapid reinforcement learning on a single machine
Use the while loop to calculate the odd and even sums in 1-100 [method 2]
Jeu de boutons de force 4 (version MySQL)
File i/o
How C # aboutbox displays its defined interface
001 new construction project based on opencvsharp
Use the for loop to calculate n! Value of
promise错误捕获处理——Promisifying技术
个人的股票交易经验
vb屏幕分辨率设置和获取_hawkol_新浪博客
Li Kou: Change
GDB debugging practice (8) transfer startup parameters to the program
【深入理解TcaplusDB技術】TcaplusDB構造數據
力扣刷題集結4(mysql版本)
LeetCode-543-二叉树的直径
.bmp图片的文件头解析
左手代码,右手开源,开源路上的一份子
Font size in DataGridView of C #
软件测试----测试的分类
Pi4j pin analog bus, several ideas of control transmission and data transmission