1

我有一个 DataGrid,它当前有一个 ItemsSource="{Binding Events}",在我需要绑定到不属于 ItemsSource="{Binding Events}" 的属性 (OccurredOnLocal) 的列中。我该怎么做?

EventHistoryView.xaml

<sdk:DataGrid x:Name="dataGrid" Grid.Row="3" Margin="5,5" ItemsSource="{Binding Events}"  IsReadOnly="True" AutoGenerateColumns="False" IsEnabled="{Binding DataIsCurrent}">
        <i:Interaction.Behaviors>
            <behaviors:DataGridDoubleClickBehavior DoubleClickCommand="{Binding DoubleClickCommand}" CommandParameter="{Binding SelectedItem, ElementName=dataGrid}"/>
        </i:Interaction.Behaviors>
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn Header="Priority" Binding="{Binding Priority}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="auto"/>
            <sdk:DataGridCheckBoxColumn Header="Requires Ack" Binding="{Binding RequiresAcknowledgement}" Width="auto"/>
            <sdk:DataGridTextColumn Header="Occurred On" Binding="{Binding MessageTime}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="*"/>
            <sdk:DataGridTextColumn Header="Occurred On (Local)" Binding="{Binding OccurredOnLocal}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="*"/>
            <sdk:DataGridTemplateColumn Header="Alarm" Width="*">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Border Background="{Binding DisplayColour}">
                            <TextBlock Text="{Binding EventDisplayText}" VerticalAlignment="Center"/>
                        </Border>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>
            <sdk:DataGridTextColumn Header="Controller" Binding="{Binding ControllerDisplayText}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="*"/>
            <sdk:DataGridTextColumn Header="Reader" Binding="{Binding ReaderDisplayText}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="*"/>
            <sdk:DataGridTextColumn Header="Card Number" Binding="{Binding BadgeId}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="auto"/>
            <sdk:DataGridTextColumn Header="Person" Binding="{Binding PersonDisplayText}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="*"/>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>

EventHistoryViewModel.cs

    #region OccurredOnLocal Property
    /// <summary>
    /// The <see cref="OccurredOnLocal" /> property's name.
    /// </summary>
    public const string OccurredOnLocalPropertyName = "OccurredOnLocal";

   private TimeZoneInfo occurredOnLocal = TimeZoneInfo.Local;

    /// <summary>
    /// Gets the OccurredOnLocal property.
    /// TODO Update documentation:
    /// Changes to that property's value raise the PropertyChanged event. 
    /// This property's value is broadcasted by the Messenger's default instance when it changes.
    /// </summary>
    public TimeZoneInfo OccurredOnLocal
    {
        get
        {
            return occurredOnLocal;
        }

        set
        {
            if (occurredOnLocal == value)
            {
                return;
            }

            occurredOnLocal = value;

            RaisePropertyChanged(OccurredOnLocalPropertyName);
        }
    }
    #endregion

谢谢,
杰森

4

1 回答 1

2

您可以使用RelativeSourcexaml 扩展

<sdk:DataGridTextColumn Header="Occurred On (Local)" Binding="{Binding DataContext.OccurredOnLocal, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
于 2012-12-07T15:45:09.297 回答