« DataContext » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
(Aucune différence)

Version du 28 mars 2017 à 14:02

Dans le code behind de la vue

Csharp.svg
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainWindowVM();
    }

Dans la vue

Xaml.svg
<Window x:Class="WpfApplication1.MainWindow"
        xmlns:local="clr-namespace:WpfApplication">
    <Window.DataContext>
        <local:MainWindowVM />  <!-- appel le ctor par défaut de MainWindowVM -->
    </Window.DataContext>

Via une StaticResource

Xaml.svg
<Window x:Class="WpfApplication1.MainWindow"
        xmlns:local="clr-namespace:WpfApplication"
        DataContext="{Binding Source={StaticResource ResourceKey=MainVM}}">
    <Window.Resources>
        <local:MainWindowVM x:Key="MainWindowVM" />
    </Window.Resources>