C#
Файл MyLocator.cs
namespace WpfApp1
{
class MyLocator
{
public MainWindowViewModel myMainWindowViewModel { get; } = new MainWindowViewModel();
}
}
C#
Файл MainWindowViewModel.cs
namespace WpfApp1
{
class MainWindowViewModel
{
public MainWindowViewModel()
{
BookName = "Hello";
}
public string BookName { get; set; }
}
}
Xaml
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<local:MyLocator x:Key="KeyForMyLocator" />
</ResourceDictionary>
</Application.Resources>
</Application>
Xaml
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
DataContext="{Binding Source={StaticResource KeyForMyLocator}, Path=myMainWindowViewModel}"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<TextBlock
Text="{Binding BookName}"
HorizontalAlignment="Left"
FontSize="22">
</TextBlock>
</StackPanel>
</Window>