WPF learning diary -- binding ViewModel and wpfviewmodel to DataContext of Window

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.