更新时间:2023-01-17 08:04:06
当XAML被处理/执行时,它正在实例化所有类。< DockPanel> ...< / DockPanel>
被实例化, 所以< Button> ...< /按钮>
以及< c:MyData x:Key =myDataSource/> !!
Hi, i have a question about data binding in WPF. From Microsoft's documentation,here: http://msdn.microsoft.com/en-us/library/ms752347.aspx[^] there is something that bugs me about the source class.
<DockPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:SDKSample">
<DockPanel.Resources>
<c:MyData x:Key="myDataSource"/>
</DockPanel.Resources>
<DockPanel.DataContext>
<Binding Source="{StaticResource myDataSource}"/>
</DockPanel.DataContext>
<Button Background="{Binding Path=ColorName}"
Width="150" Height="30">I am bound to be RED!</Button>
</DockPanel>
Here we have a source class of MyData. But as a class alone it's not instantiated. So we don't know if it's Binding Path = ColorName is red. But it gives the color red to the button. So my question is how did that happen without instantiation? Was that a static class or we have a code behind that Microsoft didn't show that instantiates MyData?
When the XAML is processed/executed, it is instantiating all of the classes.
The<DockPanel> ... </DockPanel>
is instantiated, so is the<Button>...</Button>
and so is the <c:MyData x:Key="myDataSource"/>!!