In the XAML fragment below, ItemsSource is set correctly for “testGrid” but for “testGridResource” it is null. How do I set the ItemsSource of the grid defined as a resource via a binding? More generally, how do I set a property of an object defined as a static resource via a binding (I am only using datagrid as an example in my app I am using other objects)?
<Window x:Class="StackedBarTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ig="http://schemas.infragistics.com/xaml"
Title="MainWindow" Height="350" Width="525" x:Name="root">
<Window.Resources>
<DataGrid x:Key="testGridResource" ItemsSource="{Binding LoadStatistics, ElementName=root}"></DataGrid>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<DataGrid x:Name="testGrid" ItemsSource="{Binding LoadStatistics, ElementName=root}"></DataGrid>
Basically, the answer is this: If a resource is not used in xaml it is not parsed so properties that are targets of bindings will appear null. So the bindings that were posted above are all correct however since the resource was never used in xaml it was never parsed, thus the ItemSource appeared null.
See this link:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7aed2cbf-1980-4f3d-8354-83227662f428