使用DataGrid绑定到Property和ItemSsource

时间:2012-12-07 15:37:14

标签: c# silverlight xaml binding

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

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

谢谢,
杰森

1 个答案:

答案 0 :(得分:2)

您可以使用RelativeSource xaml扩展

<sdk:DataGridTextColumn Header="Occurred On (Local)" Binding="{Binding DataContext.OccurredOnLocal, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>