C#数据绑定和invoke区别
时间: 2023-08-10 21:47:53 浏览: 46
C#中的数据绑定和`Invoke`方法是两个完全不同的概念。
数据绑定是一种将数据源与显示控件进行关联的技术,使得当数据源的值发生改变时,显示控件会自动更新相应的内容。数据绑定可以通过代码或者可视化界面的方式进行设置,常用于图形界面开发中。
`Invoke`方法则是用于在多线程编程中,将指定的代码块异步地调用到创建`Control`的线程上执行。这是因为在WinForms或WPF等UI框架中,只能在创建控件的线程上访问控件,而不能在其他线程上直接访问。如果需要在其他线程上进行操作,就需要使用`Invoke`方法将代码块发送到创建控件的线程上执行。
因此,数据绑定和`Invoke`方法是两个完全不同的概念,没有可比性。
相关问题
用C#代码怎么实现WPF中实现不同页面相同ComboBox控件绑定数据源数据的同步
好的,以下是用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; }
set
{
_itemsSource = value;
OnPropertyChanged("ItemsSource");
}
}
public string SelectedItem
{
get { return _selectedItem; }
set
{
_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
{
get
{
if (_instance == null)
{
_instance = new MyViewModel();
}
return _instance;
}
}
public ObservableCollection<string> ItemsSource
{
get { return _itemsSource; }
set
{
_itemsSource = value;
OnPropertyChanged("ItemsSource");
}
}
public string SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
```
这样就可以在不同页面中实现ComboBox控件绑定数据源数据的同步了。需要注意的是,要确保ViewModel的生命周期和页面的生命周期一致,否则可能会出现数据同步不成功的情况。
C# Winform DataGrid 绑定List<> Or ObservableCollection<> 类型无法自动刷新问题
在C# Winform中,如果你要绑定一个List<>或ObservableCollection<>类型的数据到DataGrid控件中,当你修改了数据源中的数据时,界面不会自动刷新。
解决这个问题的方法是在数据源上实现INotifyPropertyChanged接口,并在属性值发生变化时触发PropertyChanged事件。这样当数据源中的数据发生变化时,DataGrid控件会自动刷新。
以下是一个示例代码:
```csharp
public class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
if (value != name)
{
name = value;
OnPropertyChanged("Name");
}
}
}
private int age;
public int Age
{
get { return age; }
set
{
if (value != age)
{
age = value;
OnPropertyChanged("Age");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
```
在这个示例中,Person类实现了INotifyPropertyChanged接口,并在Name和Age属性的setter方法中触发了PropertyChanged事件。
接下来,你可以将Person对象的集合绑定到DataGrid控件中:
```csharp
List<Person> personList = new List<Person>();
personList.Add(new Person() { Name = "Tom", Age = 18 });
personList.Add(new Person() { Name = "Jerry", Age = 20 });
dataGridView1.DataSource = personList;
```
现在,当你修改Person对象的属性值时,DataGrid控件会自动刷新。