,其具有以下XAML中MainWindow.xaml:默認的DataContext
<Window x:Class="TestDependency.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label Name="someLabel" Grid.Row="0" Content="{Binding Path=LabelText}"></Label>
<Button Grid.Row="2" Click="Button_Click">Change</Button>
</Grid>
</Window>
而且在MainWindow.xaml.cs後面的下面的代碼:
public static readonly DependencyProperty LabelTextProperty = DependencyProperty.Register("LabelText", typeof(String), typeof(MainWindow));
public int counter = 0;
public String LabelText
{
get
{
return (String)GetValue(LabelTextProperty);
}
set
{
SetValue(LabelTextProperty, value);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
LabelText = "Counter " + counter++;
}
我本來以爲默認DataContext
是背後的代碼。但我不得不指定DataContext
。 其中DataContext
是默認的?Null
?我會認爲後面的代碼會是(就像同一個類)。
而作爲此示例中,我使用後面的代碼修改標籤的內容,可能我直接用:
someLabel.Content = "Counter " + counter++;
我會想到的是被後面的代碼,它不應該有如果DataContext
位於不同的類中,則表示UI更新問題。
雖然如此,綁定/ datacontext是從父母沒有指定時繼承。否則,在窗口類上設置datacontext將不會產生結果。 –
@MiyamotoAkira:當然,它是繼承的(DataContext的文檔也是這樣說的)。但是你爲什麼期望你的'MainWindow'默認是任何東西的'DataContext'?綁定引擎無法讀取您的想法。 – Jon
我想,因爲我看到'MainWindow'作爲這個程序的層次結構的頂部。但接下來,我不知道wpf在做什麼(但是:-)),並且可能還有其他的東西。 –