如何在datagrid中添加资源作为绑定?

时间:2011-12-27 10:34:10

标签: wpf xaml data-binding datagrid resources

这是我的代码的一部分:

<ribbon:RibbonWindow x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    x:Name="RibbonWindow"
Width="850" Height="600">

    <Window.Resources>
        <ResourceDictionary>
            <sys:Int32 x:Key="Timestep">0</sys:Int32>

            <CollectionViewSource x:Key="cvsNodes" />
        </ResourceDictionary>
    </Window.Resources>

    ...

    <DataGrid x:Name="NodeDataGrid" Grid.Row="1" AutoGenerateColumns="False" AlternatingRowBackground="Linen" IsReadOnly="True" ItemsSource="{Binding Source={StaticResource cvsNodes}}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Node" Binding="{Binding Path=ID}" SortDirection="Ascending" />
            <DataGridTextColumn Header="X" Binding="{Binding Path=Position.X, StringFormat=\{0:F\}}" />
            <DataGridTextColumn Header="Y" Binding="{Binding Path=Position.Y, StringFormat=\{0:F\}}" />
            <DataGridTextColumn Header="Z" Binding="{Binding Path=Position.Z, StringFormat=\{0:F\}}" />
            <DataGridTextColumn Header="Pressure (s)" Binding="{Binding Path=Results[{DynamicResource Timestep}].PressureSupply, StringFormat=\{0:F\}}" />
        </DataGrid.Columns>
    </DataGrid>

    ...
</ribbon:RibbonWindow>

一切正常,除了这一行:

<DataGridTextColumn Header="Pressure (s)" Binding="{Binding Path=Results[{DynamicResource Timestep}].PressureSupply, StringFormat=\{0:F\}}" />

然而,这有效(我将{DynamicResource Timestep}替换为0):

<DataGridTextColumn Header="Pressure (s)" Binding="{Binding Path=Results[0].PressureSupply, StringFormat=\{0:F\}}" />

我如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

据我所知,索引器必须是编译时常量,所以没有办法做你正在尝试的事情。解决方法将取决于您的确切要求,但您可以拥有一个具有众所周知名称的属性,该属性根据当前时间步骤公开正确的结果。