public
static
readonly
DependencyProperty
NameProperty
=
DependencyProperty
.
Register
(
"Name"
,
typeof
(
string
)
,
typeof
(
Student
)
)
;
public
string
Name
set
{
SetValue
(
NameProperty
,
value
)
;
}
get
{
return
(
string
)
GetValue
(
NameProperty
)
;
}
public
static
readonly
DependencyProperty
AgeProperty
=
DependencyProperty
.
Register
(
"Age"
,
typeof
(
int
)
,
typeof
(
Student
)
)
;
public
int
Age
set
{
SetValue
(
AgeProperty
,
value
)
;
}
get
{
return
(
int
)
GetValue
(
AgeProperty
)
;
}
public
override
string
ToString
(
)
return
this
.
Name
;
ViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WpfDependencyApp.Model;
namespace WpfDependencyApp.ViewModel
public class MainViewModel
private List<Student> students = new List<Student>();
public MainViewModel()
Students = new List<Student>()
new Student{Name="chen1",Age=100},
new Student{Name="chen2",Age=101},
ButtonCommand = new RelayCommand(() =>
testCommand();
});
Selected = Students[0];
public List<Student> Students { get => students; set => students = value; }
private Student selected = new Student();
public Student Selected { get => selected; set => selected = value; }
public ICommand ButtonCommand { get; private set; }
public void testCommand()
this.Selected.Age = 102;
public class RelayCommand : ICommand
public event EventHandler CanExecuteChanged = (sender, e) => { };
private Action mAction;
public RelayCommand(Action action)
this.mAction = action;
public bool CanExecute(object parameter)
return true;
public void Execute(object parameter)
mAction.Invoke();
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfDependencyApp.ViewModel;
namespace WpfDependencyApp
public partial class MainWindow : Window
public MainWindow()
InitializeComponent();
this.DataContext = new MainViewModel();
xaml.cs
<Window x:Class="WpfDependencyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfDependencyApp"
xmlns:test="clr-namespace:WpfDependencyApp.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<ComboBox x:Name="test" ItemsSource="{Binding Students}" SelectedItem="{Binding Selected}" DisplayMemberPath="Age"/>
<Button Height="40" Command="{Binding ButtonCommand}"></Button>
</StackPanel>
</Window>
Student.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;namespace WpfDependencyApp.Model{ public class Student:DependencyObject { public static.
1、数据
绑定
前台代码:
<
ComboBox Height="23" HorizontalAlignment="Left" Margin="86,143,0,0" Name="
comboBox1" VerticalAlignment="Top" Width="120">
</
ComboBox>
后台代码:
class ProductImg //声明类
wpf中关于按钮Button、菜单项MenuItem等关于点击交互的事件,可以通过命令Command在ViewModel 中实现。将控件属性或者控件本,当做参数传入在CommandParameter中绑定ElementName对应控件的name,和属性名称。
除了点击事件通过Command绑定之外,想要绑定其他命令如MouseEnter、SelectionChanged等事件,则需要导入专门的nuget包,安装System.Windows.Interactivity.WPF包
ComboBox是一个非常常用的下拉菜单界面控件,它的
数据源ItemsSource既可以
绑定一个List列表,也可以是一个字典,本篇文章就讲这些内容展开讲解。
首先,讲解几个常用的属性概念:
ItensSource:用于指定下拉列表
绑定的List数据对象;
SelectedIndex :下拉列表中选中行的索引;
DisplayMe
思路:给ComboBox控件设置它的ItemSource绑定到ViewModel中的某个列表上,该列表是某个实体类的集合(如List< Person >),而ComboBox列表要显示的是该实体类的某一属性(如person.Name)。
大致步骤:
联网获取到这组数据的Json,然后反序列化为对应的List< 实体类 >列表。
由于只想要绑定这组实体类的Name属性,所...
第一部分参考文章:https://www.cnblogs.com/fdyang/p/3459309.html1. 在UI(Xaml) 里面直接绑定数据.(不实用)【可运行】<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/prese...
本文主要探讨
ComboBox的
数据源设置、选中数据的
绑定(SelectedItem和SelectedValue用法)的问题
绑定数据源 &
设置显示路径
<
ComboBox ItemsSource="{Binding Fruits}" DisplayMemberPath="Name" ></
ComboBox>
ComboBox单项的数据结构为FruitViewModel
public class FruitViewModel: INotifyPropertyChanged
最近几天高强度开发,暴露出不少问题,WPF还达不到信手拈来的地步,好些东西还要去看看以前的项目。平时还是要多总结的,层次高了之后关注的知识点才会更深入。下面总结下WPF的绑定相关,总结之前又看了一遍深入浅出WPF,结合平时用到的得出此文(以TextBox为例,覆盖常见的需求,其他控件类似,代码下载,先看代码再看解释效果更好)。
本文主要包含以下内容:
1.Tex
UI<UserControl x:Class="UnitViews.UserMeUV"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
好的,以下是用C#代码实现WPF中实现不同页面相同ComboBox控件绑定数据源数据的同步的方法:
1. 首先,创建一个ViewModel类,并在其中定义ComboBox的数据源和选中项属性,同时实现INotifyPropertyChanged接口,如下所示:
public class MyViewModel : INotifyPropertyChanged
private ObservableCollection<string> _itemsSource;
private string _selectedItem;
public ObservableCollection<string> ItemsSource
get { return _itemsSource; }
_itemsSource = value;
OnPropertyChanged("ItemsSource");
public string SelectedItem
get { return _selectedItem; }
_selectedItem = value;
OnPropertyChanged("SelectedItem");
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
2. 在每个页面的代码中,创建一个MyViewModel实例,并将ComboBox的ItemsSource和SelectedValue属性分别绑定到ViewModel中对应的属性,如下所示:
public partial class Page1 : Page
private MyViewModel _viewModel;
public Page1()
InitializeComponent();
_viewModel = MyViewModel.Instance;
comboBox1.ItemsSource = _viewModel.ItemsSource;
comboBox1.SetBinding(ComboBox.SelectedValueProperty, new Binding("SelectedItem") { Mode = BindingMode.TwoWay });
public partial class Page2 : Page
private MyViewModel _viewModel;
public Page2()
InitializeComponent();
_viewModel = MyViewModel.Instance;
comboBox1.ItemsSource = _viewModel.ItemsSource;
comboBox1.SetBinding(ComboBox.SelectedValueProperty, new Binding("SelectedItem") { Mode = BindingMode.TwoWay });
3. 在MyViewModel类中使用Singleton模式创建一个实例,确保所有页面使用的是同一个ViewModel对象,如下所示:
public class MyViewModel : INotifyPropertyChanged
private static MyViewModel _instance;
private ObservableCollection<string> _itemsSource;
private string _selectedItem;
public static MyViewModel Instance
if (_instance == null)
_instance = new MyViewModel();
return _instance;
public ObservableCollection<string> ItemsSource
get { return _itemsSource; }
_itemsSource = value;
OnPropertyChanged("ItemsSource");
public string SelectedItem
get { return _selectedItem; }
_selectedItem = value;
OnPropertyChanged("SelectedItem");
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
这样就可以在不同页面中实现ComboBox控件绑定数据源数据的同步了。需要注意的是,要确保ViewModel的生命周期和页面的生命周期一致,否则可能会出现数据同步不成功的情况。