当前位置:网站首页>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
边栏推荐
- WPF 路由
- Verilog参数例化时自动计算位宽的函数
- GDB调试实战(10)多线程调试
- [deeply understand tcapulusdb technology] tcapulusdb import data
- 电脑屏幕分辨率怎么调?电脑屏幕修改分辨率SwitchResX
- WPF 手写板
- Mafft|multi sequence alignment tool
- KVM虚拟机救援模式修改root密码 —— 筑梦之路
- University of Virginia: ingy Elsayed aly | logic based reward formation in Multi-Agent Reinforcement Learning
- Pycharm使用指南
猜你喜欢
随机推荐
Using bioedit to do multiple sequence consistency alignment
力扣:零钱兑换
C# DataTable转换为Entity(反射&&泛型)
UEFI dual system + dual hard disk installation
Use the while loop to calculate the odd and even sums in 1-100 [method 1]
分别利用for、while、do while,循环求1-100的和
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
Use the while loop to calculate the odd and even sums in 1-100 [method 2]
Jeu de boutons de force 4 (version MySQL)
js 监听和移除监听事件
【深入理解TcaplusDB技术】Tmonitor后台一键安装
Is it OK for Zhongyan futures to open an account? Is the platform reliable? Is it safe?
利用for循环,分别计算1-100中奇数的和、偶数的和【方法二】
GDB debugging skills (0) getting started with GDB
[deeply understand tcapulusdb technology] one click installation of tmonitor background
Qt滚动区域QScrollArea
Use for, while and do while to find the sum of 1-100 circularly
个人的股票交易经验
UWP Dispatcher用法
IP-guard打印管控,防止打印渠道信息泄露



![[deeply understand tcapulusdb technology] table management of document acceptance](/img/59/e435623ab9cc7d2aa164c2f08e135f.png)





