1

在下面的 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>
4

3 回答 3

0

看看这是否有助于回答您的问题:绑定到静态属性

于 2012-11-26T17:10:52.647 回答
0

所以你只想将你的 DataGrid 绑定到 testGridResource,如果是这样的话,这应该可以

<DataGrid x:Name="testGrid" ItemsSource="{Binding Source={StaticResource testGridResource}}">

不确定这是否是您想要的

也许是这样:

<DataGrid x:Name="testGridResource" ItemsSource="{Binding Path=LoadStatistics, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}
于 2012-11-26T20:54:05.947 回答
0

基本上,答案是这样的:如果 xaml 中未使用资源,则不会对其进行解析,因此作为绑定目标的属性将显示为空。因此,上面发布的绑定都是正确的,但是由于该资源从未在 xaml 中使用过,因此从未解析过,因此 ItemSource 显示为空。

请参阅此链接:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7aed2cbf-1980-4f3d-8354-83227662f428

于 2012-12-05T01:00:51.180 回答