using System.ComponentModel;
namespace TestBinding
{
public class Service : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int id;
private InfoService info;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public int Id
{
get { return id; }
set
{
id = value;
RaisePropertyChanged("Id");
}
}
public InfoService Info
{
get { return info; }
set
{
info = value;
RaisePropertyChanged("Info");
}
}
}
public class InfoService : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int id;
private ServiceMain main;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public int Id
{
get { return id; }
set
{
id = value;
RaisePropertyChanged("Id");
}
}
public ServiceMain Main
{
get { return main; }
set
{
main = value;
RaisePropertyChanged("Main");
}
}
}
public class ServiceMain : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int id;
private string name;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public int Id
{
get { return id; }
set
{
id = value;
RaisePropertyChanged("Id");
}
}
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged("Name");
}
}
}
}
using System.Windows;
namespace TestBinding
{
public partial class MainWindow : Window
{
public Service service;
public MainWindow()
{
InitializeComponent();
service =
new Service{ Id = 0, Info =
new InfoService{ Id = 1, Main =
new ServiceMain { Id = 2, Name="Тест"} } };
}
}
}
<Window x:Class="TestBinding.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:TestBinding"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel DataContext="{Binding Source=service}">
<TextBlock Text="Service"/>
<TextBox Text="{Binding Path=Id}"/>
<TextBlock Text="InfoService"/>
<TextBox Text="{Binding Path=Id}"/>
<TextBlock Text="ServiceMain"/>
<TextBox Text="{Binding Path=Id}"/>
</StackPanel>
</Window>
private void Application_Startup(object sender, StartupEventArgs e)
{
MainWindow = new MainWindow
{
DataContext = new Service
{
Id = 0,
Info = new InfoService
{
Id = 1,
Main = new ServiceMain { Id = 2, Name = "Тест"}
}
}
};
MainWindow.Show();
}
<StackPanel>
<TextBlock Text="Service"/>
<TextBox Text="{Binding Path=Id}"/>
<StackPanel DataContext={Binding Info}>
<TextBlock Text="InfoService"/>
<TextBox Text="{Binding Path=Id}"/>
<StackPanel DataContext={Binding Main}>
<TextBlock Text="ServiceMain"/>
<TextBox Text="{Binding Path=Id}"/>
</StackPanel>
</StackPanel>
</StackPanel>