Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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.

XML
<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?
Posted

1 solution

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"/>!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900