Имеется два класса, отличающихся друг от друга только тем, что один из них реализует интерфейс INotifyPropertyChanged:
| Код | namespace WpfBinding { public class Class1 { int val; public int Value { get { return val; } set { val = value; } }
}
public class Class2 : INotifyPropertyChanged { int val; public int Value { get { return val; } set { val = value; PropertyChanged(this, new PropertyChangedEventArgs("Value")); } }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion } }
|
Имеется XAML-код окна, в котором в качестве источника используется экземпляр класса Class1:
| Код | <Window x:Class="WpfBinding.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ab="clr-namespace:WpfBinding" Title="Window1" Height="173" Width="312"> <Window.DataContext> <ab:Class2 Value="30"/> </Window.DataContext> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="50*" /> <ColumnDefinition Width="50*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="100*" /> </Grid.RowDefinitions> <Label Margin="5" Name="label1"> <Label.Content> <Binding Path="Value" Mode="OneWay"/> </Label.Content> </Label> <TextBox Grid.Column="1" Margin="5" Name="textBox1" /> <Slider Grid.ColumnSpan="2" Grid.Row="1" Height="22" Margin="5" Name="slider1" VerticalAlignment="Bottom" Minimum="0" Maximum="100" /> <Button Grid.Row="2" Margin="5" Name="button1">Button</Button> <Button Grid.Column="1" Grid.Row="2" Margin="5" Name="button2">Button</Button> </Grid> </Window>
|
При запуске приложения, label1 отображает число 30, однако, если для Window.DataContext я задам не Class1, а Class2, то тут же получаю ошибку в редакторе xaml:
| Цитата | Value '30' cannot be assigned to property 'Value'. Object reference not set to an instance of an object.
|
Почему получаю ошибку?
п.с. Администраторам: предлагаю ограничить длину строки заголовка топика до 3-5 букв, чтобы уж совсем весело было... |