Example #1
0
        private static void OnIsSelectedChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            DataGridRow row        = (DataGridRow)sender;
            bool        isSelected = (bool)e.NewValue;

            if (isSelected && !row.IsSelectable)
            {
                throw new InvalidOperationException(SR.Get(SRID.DataGridRow_CannotSelectRowWhenCells));
            }

            DataGrid grid = row.DataGridOwner;

            if (grid != null && row.DataContext != null)
            {
                DataGridAutomationPeer gridPeer = UIElementAutomationPeer.FromElement(grid) as DataGridAutomationPeer;
                if (gridPeer != null)
                {
                    DataGridItemAutomationPeer rowItemPeer = gridPeer.GetOrCreateItemPeer(row.DataContext);
                    if (rowItemPeer != null)
                    {
                        rowItemPeer.RaisePropertyChangedEvent(
                            System.Windows.Automation.SelectionItemPatternIdentifiers.IsSelectedProperty,
                            (bool)e.OldValue,
                            isSelected);
                    }
                }
            }

            // Update the header's IsRowSelected property
            row.NotifyPropertyChanged(row, e, NotificationTarget.Rows | NotificationTarget.RowHeaders);

            // This will raise the appropriate selection event, which will
            // bubble to the DataGrid. The base class Selector code will listen
            // for these events and will update SelectedItems as necessary.
            row.RaiseSelectionChangedEvent(isSelected);
        }