I am loading some XAML pages that I have created that have some data binding in them. However, when I load these XAML files at runtime their data binding no longer works. I'm wondering if it's some kind of scope or pathing issue or if you just can't do it.
Here is how I am loading the XAML
using (XmlReader rdr = XmlReader.Create(@".\TwitterModule\TwitterModule.xaml"))
{
Canvas twitter = XamlReader.Load(rdr) as Canvas;
contentRoot.Children.Add(twitter);
}
And here is the my final XAML
<!--XAML -->
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:LocalTrendsDS="clr-namespace:LocalTrendsDS;assembly=LocalTrendsDS" />
<Canvas.Resources>
<XmlDataProvider x:Key="TweetCounter" Source="..\temp\TweetCounter.xml" />
<LocalTrendsDS:LocalTrends x:Key="LocalTrendsDataSource" />
</Canvas.Resources>
<Canvas x:Name="root" Height="479" Width="877"
DataContext="{Binding Source={StaticResource TweetCounter}}">
<TextBlock x:Name="Number"
Canvas.Left="86.744" LineHeight="122.919" TextAlignment="Right" TextWrapping="Wrap" Width="425.372"
Text="{Binding Mode=OneWay, XPath=/Count}" />
</Canvas>
<Canvas x:Name="_3a" Height="383" Canvas.Left="1971" Canvas.Top="107" Width="362"
DataContext="{Binding Source={StaticResource LocalTrendsDataSource}}">
<TextBlock x:Name="TrendList" FontSize="16.449" Canvas.Left="-0.806" LineHeight="16.449" TextAlignment="Left" TextWrapping="Wrap" Canvas.Top="112.155" Height="127.829"><TextBlock.RenderTransform>
<MatrixTransform Matrix="2.334,0,0,2.334,0,0"/>
</TextBlock.RenderTransform>
<Run Foreground="#FF9900EB" FontFamily="Hiruko Alternate" Text="1. "/>
<TextBlock Foreground="#FFFFFFFF" FontFamily="Segoe Semibold" Text="{Binding CurrentLocalTrends[0].Value, Mode=Default}"/><LineBreak/>
<Run Foreground="#FFDB0072" FontFamily="Hiruko Alternate" Text="2. "/>
<TextBlock Foreground="#FFFFFFFF" FontFamily="Segoe Semibold" Text="{Binding CurrentLocalTrends[1].Value, Mode=Default}"/><LineBreak/>
<Run Foreground="#FFFF0F1B" FontFamily="Hiruko Alternate" Text="3. "/>
<TextBlock Foreground="#FFFFFFFF" FontFamily="Segoe Semibold" Text="{Binding CurrentLocalTrends[2].Value, Mode=Default}"/><LineBreak/>
<Run Foreground="#FFFF6E05" FontFamily="Hiruko Alternate" Text="4. "/>
<TextBlock Foreground="#FFFFFFFF" FontFamily="Segoe Semibold" Text="{Binding CurrentLocalTrends[3].Value, Mode=Default}"/><LineBreak/>
<Run Foreground="#FFFF9F01" FontFamily="Hiruko Alternate" Text="5. "/>
<TextBlock Foreground="#FFFFFFFF" FontFamily="Segoe Semibold" Text="{Binding CurrentLocalTrends[4].Value, Mode=Default}"/><LineBreak/>
</TextBlock>
</Canvas>
</Canvas>
ANSWER: It was a pathing issue, thanks for your help. I'm able to bind both XmlDataProvider and other Object Data Sources in XAML loaded at runtime. Huge time saver. Build UI in Blend and in the darkness bind them.