2

不久前,我为 XamDataGrid 和作为 ObservableCollection 的“业务对象”之间的两种方式同步编写了一个“附加行为”。XamDataGrid 是源,ObservableCollection as DataSource 是目标。由于特定原因,我没有使用 ListCollectionView。

问题

当 DataGrid 的 DataContext 更改为另一个 Vehicle 时,当前加载的 DataGrid 不会更新行为的 DependencyProperty。

我不知道为什么。

我能想到的唯一解决方案是挂钩 DataGrid 的 DataContextChanged 并使用 Path 进行新的 BindingOperation ,然后将其设置为相对于 DataContext 以确定 SelectedItems 属性。但在这种情况下,行为的 DependencyProperty 应设置为 SelectedItems 属性的路径,而不是绑定。

有以下课程

示例模型

public class Vehicle 
{ 
    public PassengerList Passengers { get; set; } 
}

public class PassengerList : ObservableCollection<Passenger> 
{
    public PassengerList()
    {
         SelectedPassengers = new ObservableCollection<Passenger>();
    }

    public ObservableCollection<Passenger> SelectedPassengers { get; private set; }
}

public class Passenger
{
    public string Name { get; set; }
}

xml

<igDG:XamDataGrid DataSource="{Binding Passengers}">
<i:Interaction.Behaviors>
   <b:XamDataGridSelectedItemsBehavior SelectedItems="{Binding Path=Passengers.SelectedPssengers}" />
</i:Interaction.Behaviors>
</igDG:XamDataGrid>

PS:我也尝试过将元素绑定到 DataGrid 作为元素,但这并不能解决问题。DependencyProperty 仅设置一次。例如模型

双向行为

当模型中的选定项更改时,网格选定项也必须更新。它也不需要使用弱事件分离。

public class XamDataGridSelectedItemsBehavior : Behavior<XamDataGrid>, IWeakEventListener
{
    #region Properties

    private XamDataGrid Grid
    {
        get { return AssociatedObject as XamDataGrid; }
    }

    public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(
        "SelectedItems",
        typeof(INotifyCollectionChanged),
        typeof(XamDataGridSelectedItemsBehavior),
        new UIPropertyMetadata(new PropertyChangedCallback(OnSelectedItemsChanged)));

    private static void OnSelectedItemsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
    {
         if (obj != null) 
         {
            (obj as XamDataGridSelectedItemsBehavior).SelectedItems = (e.NewValue as INotifyCollectionChanged);
        }
    }

    public INotifyCollectionChanged SelectedItems
    {
        get { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); }
        set
        {
            // remove old listener
            if (SelectedItems != null)
                CollectionChangedEventManager.RemoveListener(SelectedItems, this);

            SetValue(SelectedItemsProperty, value);

            // add new listener
            if (SelectedItems != null)
                CollectionChangedEventManager.AddListener(SelectedItems, this);
        }
    }

    #endregion

    #region Init

    /// <summary>
    /// Hook up event listeners to the associated object.
    /// </summary>
    protected override void OnAttached()
    {
        base.OnAttached();

        SelectedItemsChangedEventManager.AddListener(Grid, this);
        XamDataGridRecordActivatedEventManager.AddListener(Grid, this);
        XamDataGridLoadedEventManager.AddListener(Grid, this);
    }

    void Grid_RecordActivated(object sender, RecordActivatedEventArgs e)
    {
        if (_transferingToTarget)
            return;

        // if the CellClickAction is EnterEditModeIfAllowed, the grid does not always select the actual record
        // In our case we want it to always select the record
        if (e.Record.DataPresenter.FieldSettings.CellClickAction == CellClickAction.EnterEditModeIfAllowed)
        {
            TransferSourceToTarget();
        }
    }

    void Grid_Loaded(object sender, RoutedEventArgs e)
    {
        TransferTargetToSource(true);
    }

    #endregion

    #region Target to Source

    /// <summary>
    /// When selected items in the target as model has changed, then transfer selected item to grid as the source.
    /// Not when transfering from grid to selected items.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (_transferingToTarget)
            return;

        TransferTargetToSource(false);
    }

    private bool _transferingToSource = false;
    /// <summary>
    /// Transfer selected item in the target as model to the grid as source.
    /// </summary>
    private void TransferTargetToSource(bool notifyTargetListeners)
    {
        if (SelectedItems == null)
            return;

        List<Record> newSelection = new List<Record>();
        foreach (var item in (SelectedItems as IList))
        {
            var record = Grid.Records.FirstOrDefault(r => (r is DataRecord) && ((r as DataRecord).DataItem == item));
            if (record != null)
            {
                newSelection.Add(record);
            }
        }

        _transferingToSource = true;
        try
        {
            Grid.SelectedItems.Records.Clear();
            Grid.SelectedItems.Records.AddRange(newSelection.ToArray());
            if ((newSelection.Count > 0) && !newSelection.Contains(Grid.ActiveRecord))
            {
                Grid.ActiveRecord = newSelection.FirstOrDefault();
                Grid.ActiveRecord.IsSelected = true;
            }

            if (notifyTargetListeners)
            {
                // Hack to notify the target listeners
                (SelectedItems as IList).Clear();
                foreach (var record in newSelection)
                {
                    (SelectedItems as IList).Add((record as DataRecord).DataItem);
                }
            }
        }
        finally
        {
            _transferingToSource = false;
        }
    }

    #endregion

    #region Source to Target

    /// <summary>
    /// When selected items in the source as grid has changed, then transfer selected item to model as the target.
    /// Not when transfering from selected items to grid.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void Grid_SelectedItemsChanged(object sender, SelectedItemsChangedEventArgs e)
    {
        if (_transferingToSource)
            return;

        TransferSourceToTarget();
    }

    private bool _transferingToTarget = false;
    /// <summary>
    /// Transfer the selected item in the grid as source to the selected item in the target as model.
    /// </summary>
    private void TransferSourceToTarget()
    {
        var target = this.SelectedItems as IList;
        if (target == null)
            return;

        _transferingToTarget = true;
        try
        {
            // clear the target first
            target.Clear();

            // When no item is selected there might still be an active record
            if (Grid.SelectedItems.Count() == 0)
            {
                if (Grid.ActiveDataItem != null)
                    target.Add(Grid.ActiveDataItem);
                else if (Grid.ActiveRecord != null && Grid.ActiveRecord.IsDataRecord)
                    target.Add((Grid.ActiveRecord as DataRecord).DataItem);
                else if (Grid.ActiveCell != null && Grid.ActiveCell.Record != null && Grid.ActiveCell.Record.IsDataRecord)
                    target.Add((Grid.ActiveCell.Record as DataRecord).DataItem);
            }
            else
            {
                // foreach record in the source add it to the target
                foreach (var r in Grid.SelectedItems.Records)
                {
                    if (r.IsDataRecord)
                    {
                        target.Add((r as DataRecord).DataItem);
                    }
                }
            }
        }
        finally
        {
            _transferingToTarget = false;
        }
    }

    #endregion

    /// <summary>
    /// Receive an event and delegate it to the correct eventhandler.
    /// </summary>
    /// <param name="managerType"></param>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <returns></returns>
    bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
    {
        if (managerType == typeof(CollectionChangedEventManager))
        {
            SelectedItems_CollectionChanged(sender, e as NotifyCollectionChangedEventArgs);
            return true;
        }
        else if (managerType == typeof(SelectedItemsChangedEventManager))
        {
            Grid_SelectedItemsChanged(sender, e as SelectedItemsChangedEventArgs);
            return true;
        }
        else if (managerType == typeof(XamDataGridRecordActivatedEventManager))
        {
            Grid_RecordActivated(sender, e as RecordActivatedEventArgs);
            return true;
        }
        else if (managerType == typeof(XamDataGridLoadedEventManager))
        {
            Grid_Loaded(sender, e as RoutedEventArgs);
            return true;
        }
        return false;
    }
}

#region EventManagers

public class CollectionChangedEventManager : WeakEventManagerBase<CollectionChangedEventManager, INotifyCollectionChanged>
{
    protected override void StartListeningTo(INotifyCollectionChanged source)
    {
        source.CollectionChanged += DeliverEvent;
    }

    protected override void StopListeningTo(INotifyCollectionChanged source)
    {
        source.CollectionChanged -= DeliverEvent;
    }
}

public class XamDataGridRecordActivatedEventManager : WeakEventManagerBase<XamDataGridRecordActivatedEventManager, XamDataGrid>
{
    protected override void StartListeningTo(XamDataGrid source)
    {
        source.RecordActivated += DeliverEvent;
    }

    protected override void StopListeningTo(XamDataGrid source)
    {
        source.RecordActivated -= DeliverEvent;
    }
}

public class XamDataGridLoadedEventManager : WeakEventManagerBase<XamDataGridLoadedEventManager, XamDataGrid>
{
    protected override void StartListeningTo(XamDataGrid source)
    {
        source.Loaded += DeliverEvent;
    }

    protected override void StopListeningTo(XamDataGrid source)
    {
        source.Loaded -= DeliverEvent;
    }
}

public class SelectedItemsChangedEventManager : WeakEventManagerBase<SelectedItemsChangedEventManager, XamDataGrid>
{
    protected override void StartListeningTo(XamDataGrid source)
    {
        source.SelectedItemsChanged += DeliverEvent;
    }

    protected override void StopListeningTo(XamDataGrid source)
    {
        source.SelectedItemsChanged -= DeliverEvent;
    }
}

#endregion

#region EventManager base class

// TODO: 10-10-2011 (rdj): Deze class misschien opnemen in het frontend framework? In ieder geval zolang we nog geen .NET 4.5 gebruiken
// http://10rem.net/blog/2012/02/01/event-handler-memory-leaks-unwiring-events-and-the-weakeventmanager-in-wpf-45

/// <summary>
/// Weak event manager base class to provide easy implementation of weak event managers.
/// </summary>
/// <typeparam name="TManager">Type of the manager.</typeparam>
/// <typeparam name="TEventSource">Type of the event source.</typeparam>
public abstract class WeakEventManagerBase<TManager, TEventSource> : WeakEventManager
    where TManager : WeakEventManagerBase<TManager, TEventSource>, new()
    where TEventSource : class
{
    /// <summary>
    /// Adds a listener
    /// </summary>
    /// <param name="source">The source of the event, should be null if listening to static events</param>
    /// <param name="listener">The listener of the event. This is the class that will recieve the ReceiveWeakEvent method call</param>
    public static void AddListener(object source, IWeakEventListener listener)
    {
        CurrentManager.ProtectedAddListener(source, listener);
    }

    /// <summary>
    /// Removes a listener
    /// </summary>
    /// <param name="source">The source of the event, should be null if listening to static events</param>
    /// <param name="listener">The listener of the event. This is the class that will recieve the ReceiveWeakEvent method call</param>
    public static void RemoveListener(object source, IWeakEventListener listener)
    {
        CurrentManager.ProtectedRemoveListener(source, listener);
    }

    /// <inheritdoc/>
    protected sealed override void StartListening(object source)
    {
        StartListeningTo((TEventSource)source);
    }

    /// <inheritdoc/>
    protected sealed override void StopListening(object source)
    {
        StopListeningTo((TEventSource)source);
    }

    /// <summary>
    /// Attaches the event handler.
    /// </summary>
    protected abstract void StartListeningTo(TEventSource source);

    /// <summary>
    /// Detaches the event handler.
    /// </summary>
    protected abstract void StopListeningTo(TEventSource source);

    /// <summary>
    /// Gets the current manager
    /// </summary>
    protected static TManager CurrentManager
    {
        get
        {
            var mType = typeof(TManager);
            var mgr = (TManager)GetCurrentManager(mType);
            if (mgr == null)
            {
                mgr = new TManager();
                SetCurrentManager(mType, mgr);
            }
            return mgr;
        }
    }
}

#endregion
4

1 回答 1

3

我通过添加两个新的依赖属性来让它工作。

数据上下文属性

    public static readonly DependencyProperty DataContextProperty = DependencyProperty.Register(
        "DataContext",
        typeof(object),
        typeof(XamDataGridSelectedItemsBehavior),
        new PropertyMetadata(DataContextChanged));

    private static void DataContextChanged(object obj, DependencyPropertyChangedEventArgs e)
    {
        var behavior = obj as XamDataGridSelectedItemsBehavior;
        var binding = new Binding(behavior.Path) { Source = e.NewValue };
        BindingOperations.SetBinding(behavior, XamDataGridSelectedItemsBehavior.SelectedItemsProperty, binding);
    }

用于在 DataContext 发生更改时创建新绑定的PathProperty

    public static readonly DependencyProperty PathProperty = DependencyProperty.Register(
        "Path",
        typeof(string),
        typeof(XamDataGridSelectedItemsBehavior),
        new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(OnPathChanged)));

    private static void OnPathChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var behavior = obj as XamDataGridSelectedItemsBehavior;
        behavior.Path = e.NewValue as string;
    }

    public string Path { get; set; }

DataContext 属性在 OnAttached 中设置,以便将 DataContextChanged 事件挂接到

    protected override void OnAttached()
    {
        base.OnAttached();

        SelectedItemsChangedEventManager.AddListener(Grid, this);
        XamDataGridRecordActivatedEventManager.AddListener(Grid, this);
        XamDataGridLoadedEventManager.AddListener(Grid, this);

        BindingOperations.SetBinding(this, XamDataGridSelectedItemsBehavior.DataContextProperty, new Binding());
    }

SelectedItems 依赖属性现在是私有的并且稍作修改

    public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(
        "SelectedItems",
        typeof(INotifyCollectionChanged),
        typeof(XamDataGridSelectedItemsBehavior2),
        new UIPropertyMetadata(new PropertyChangedCallback(OnSelectedItemsChanged)));

    private static void OnSelectedItemsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var behavior = obj as XamDataGridSelectedItemsBehavior;

        if (behavior.SelectedItems != null)
            CollectionChangedEventManager.RemoveListener(behavior.SelectedItems, behavior);

        if (e.NewValue is INotifyCollectionChanged)
        {
            behavior.SelectedItems = e.NewValue as INotifyCollectionChanged;
            CollectionChangedEventManager.AddListener(behavior.SelectedItems, behavior);
        }
    }

    private INotifyCollectionChanged SelectedItems
    {
        get  { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); }
        set { SetValue(SelectedItemsProperty, value); }
    }

使用 xaml 中的行为

<igDG:XamDataGrid DataSource="{Binding Passengers}">
<i:Interaction.Behaviors>
   <b:XamDataGridSelectedItemsBehavior Path="Passengers.SelectedPassengers" />
</i:Interaction.Behaviors>
</igDG:XamDataGrid>
于 2012-11-15T13:08:33.313 回答