WPF learning diary -- binding ViewModel and wpfviewmodel to DataContext of Window
1. Global ViewModel binding:
A) set the global ViewModel (in App. xaml ):
1 <Application x:Class="MyTest.App"2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"4 xmlns:local="clr-namespace:MyTest"5 StartupUri="MainWindow.xaml">6 <Application.Resources>7 <local:MainViewModel x:Key="mainVM"/>8 </Application.Resources>9 </Application>
B) bind the global ViewModel (in MainWindow ):
1 <Window x:Class="MyTest.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:local="clr-namespace:MyTest"
5 Title="MainWindow" Height="600" Width="850" WindowStartupLocation="CenterScreen"
6 DataContext="{DynamicResource mainVM}">
7 </Window>
2. Separate ViewModel binding (in MainWindow ):
1 <Window x:Class="MyTest.MainWindow"2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"4 xmlns:local="clr-namespace:MyTest"5 Title="MainWindow" Height="600" Width="850" WindowStartupLocation="CenterScreen">6 <Window.DataContext>7 <local:MainViewModel/>8 </Window.DataContext>
9 </Window>
C # When binding WPF data to DataContext; when binding Window_Loaded data, specify DataContext to trigger another event?
After updating the database, you must manually refresh it to the background object.
The binding value is changed in WPF, and the interface will not be automatically refreshed. to automatically refresh the bound property, you need to implement the INotifyPropertyChanged interface. This is complicated and you can check it yourself.
If you do not use interfaces 2, refresh the code similar to the following.
(ComboBox) sender). GetBindingExpression (ComboBox. ItemsSourceProperty). UpdateTarget ();
How can I leave DataContext binding empty when WPF writes something in the C/S architecture?
Are you bound with the attributes of the Model?
If not, bind it first.
If yes, check whether your object is empty. Debugging is a very good solution!