views:

72

answers:

2

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.

+1  A: 

Indeed, you can do binding with dynamic XAML. I have done it in the past, and it works fine.

I am not sure why your example doesn't work... possibly it can't find your XML provider source?

I tend to do something like:

twitter.DataContext = theContextYouWantToBindTo;

when I construct the control... but I'm not sure that it matters much. It certainly seems like what you are writing should work.

When I want to make sure a data context is set properly, I do something like this:

<TextBox Text="{Binding}"/>

This is really just a debugging step, but it confirms that I have an object set in my data context.

Brian Genisio
+2  A: 

It looks like you have a typo in your XAML. Did you mean the following?

<!--XAML -->
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <Canvas.Resources>
    <XmlDataProvider x:Key="TweetCounter" Source="\temp\TweetCounter.xml" />
  </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>

Also, FYI, XmlTextReader has been deprecated since .NET 2.0. You should use the following instead:

using (XmlReader rdr = XmlReader.Create(@".\TwitterModule\TwitterModule.xaml"))
{
    Canvas twitter = XamlReader.Load(rdr) as Canvas; 
    contentRoot.Children.Add(twitter); 
}
John Saunders
+1 for XmlReader Thanks for pointing that out.
discorax