0

OnShouldRefreshPropertyChanged 只会触发一次,并且不会在 NeedsRefresh 的值更改时触发。有人可以解释发生了什么吗?我使用的 Datagrid 来自 .Net 3.5 的 WPFToolKit,尽管我认为这并不重要。

行为:

public static class DataGridRefreshBehavior
{
    // Using a DependencyProperty as the backing store for ShouldRefresh.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ShouldRefreshProperty =
        DependencyProperty.RegisterAttached("ShouldRefresh",
        typeof(bool),
        typeof(DataGridRefreshBehavior),
        new UIPropertyMetadata(false, OnShouldRefreshPropertyChanged));

    public static void SetShouldRefresh(DependencyObject obj, bool value)
    {
        if (obj != null)
            obj.SetValue(ShouldRefreshProperty, value);
    }

    public static bool GetShouldRefresh(DependencyObject obj)
    {
        if (obj != null)
            return (bool)obj.GetValue(ShouldRefreshProperty);
        else
            return false;
    }

    private static void OnShouldRefreshPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var attachedObject = sender as DataGrid;
        if (attachedObject != null)
        {
            bool needsRefresh = (bool)e.NewValue;
            if (needsRefresh)
            {
                attachedObject.Items.Refresh();
            }
        }
    }
}

xml:

local:DataGridRefreshBehavior.ShouldRefresh="{Binding Path=NeedsRefresh,UpdateSourceTrigger=PropertyChanged}"

视图模型:

public bool NeedsRefresh
    {
        get
        {
            return needsRefresh;
        }
        set
        {

            needsRefresh = value;
            NotifyOfPropertyChange(() => NeedsRefresh);

        }
    }
4

0 回答 0