在下面的XAML片段中,为“testGrid”正确设置了ItemsSource,但对于“testGridResource”则为null。如何通过绑定将网格的ItemsSource设置为资源?更一般地说,如何通过绑定将静态资源定义为对象的属性(我仅在使用其他对象的应用程序中使用datagrid作为示例)?如何通过绑定来设置静态资源的属性
<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>
你在哪里使用testGridResource?您需要设置封闭xaml元素的DataContext。 – Tilak
@Tilak DataContext在类的构造函数中设置。它与testGrid使用的datacontext相同。另外,我绑定了一个命名元素(root)的属性。 – Sam
你在哪里使用你创建的资源testGridResource? –