属性 更改时 WPF 通知内容更改

WPF Notify content change when it's property changed

我已将标签 Content 设置为一些自定义 class:

<Label>
    <local:SomeContent x:Name="SomeContent" some="abc" />
</Label>

这会在视图中正确显示 "abc"。但是我不知道如何通知 Label 内容 属性 已更改,即:

SomeContent.some = "xyz";

不会导致标签更新其视图。

我知道我可以设置绑定到标签的 Content 属性。我已经喜欢 7 种不同的工作方法来实现自动更新。但是我对这种特殊行为很感兴趣,因为它会在某些情况下为我节省大量工作,即要求是:

我是不是遗漏了什么,或者这不可能?

这是我目前对 SomeContent 的实现:

public class SomeContent : DependencyObject, INotifyPropertyChanged {

    public static readonly DependencyProperty someProperty =
        DependencyProperty.Register(nameof(some), typeof(string),
            typeof(SomeContent),
            new PropertyMetadata("", onDPChange)
    );

    private static void onDPChange(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        //throw new NotImplementedException();
        (d as SomeContent).some = e.NewValue as String;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public string some {
        get => (string)GetValue(someProperty);
        set {
            SetValue(someProperty, value);
            PropertyChanged?.Invoke(this,
                new PropertyChangedEventArgs(nameof(some))
            );
        }
    }


    public override string ToString() => some;
}

更新的答案:

我会放弃将 SomeContent 作为依赖项实现 属性 并改用 UserControl:

<UserControl x:Class="WpfApp1.SomeContent"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp1"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
            <TextBlock Text="{Binding some, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SomeContent}}}"/>
    </Grid>
</UserControl>

然后在后面的代码中:

/// <summary>
/// Interaction logic for SomeContent.xaml
/// </summary>
public partial class SomeContent : UserControl
{
    public static readonly DependencyProperty someProperty =
        DependencyProperty.Register(nameof(some), typeof(string),
            typeof(SomeContent),
            new PropertyMetadata("")
        );

    public string some
    {
        get => (string)GetValue(someProperty);
        set => SetValue(someProperty, value);
    }

    public SomeContent()
    {
        InitializeComponent();
    }
}

接下来,实现一个实现了 INotifyPropertyChanged 的​​视图模型:

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _somePropertyOnMyViewModel;
    public string SomePropertyOnMyViewModel
    {
        get => _somePropertyOnMyViewModel;
        set { _somePropertyOnMyViewModel = value; OnPropertyChanged(); }
    }
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后在您的视图中创建一个 MyViewModel 实例并将其分配给您视图的 DataContext:

public class MyView : Window
{
    public MyView()
    {
        InitializeComponent();
        DataContext = new MyViewModel();
    }
}

然后,最后,在 MyView 中使用我在原始答案中提供的标记:

<Label>
    <local:SomeContent x:Name="SomeContent" some="{Binding 
SomePropertyOnMyViewModel" />
</Label>

我发现没有第三方代码是不可能做到的。所以我现在写了一个助手 class 来轻松完成。

动态对象

public class SomeContent : IChangeNotifer {
    public event Action<object> MODIFIED;

    private string _some;
    public string some {
        get => _some;
        set {
            _some = value;
            MODIFIED?.Invoke(this);
        }
    }

    public override string ToString() => some;
}

您可以将它添加到xaml文件中,它会自动更新。一个额外的步骤是添加 UIReseter 在假定自动更新的元素下方的某处,但树中的多个内容只需要一个。

用法

<Window x:Class="DependencyContentTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DependencyContentTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <local:UIReseter />
        <Label>
            <local:SomeContent x:Name="SomeContent" some="abcd" />
        </Label>
        <Grid>
            <Label>
                <local:SomeContent x:Name="nested" some="nyest"/>
            </Label>
        </Grid>
    </StackPanel>
</Window>

主窗口代码

public partial class MainWindow : Window {

    private Timer t;

    public MainWindow() {
        InitializeComponent();
        t = new Timer(onTimer, null, 5000, Timeout.Infinite);
        MouseDown += (s,e) => { SomeContent.some = "iii"; };
    }

    private void onTimer(object state) {
        Dispatcher.Invoke(() => {
            SomeContent.some = "aaaa";
            nested.some = "xxx";
        });
    }
}

这是处理更新的助手class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using H = System.Windows.LogicalTreeHelper;
using FE = System.Windows.FrameworkElement;
using DO = System.Windows.DependencyObject;
using System.Reflection;
using System.Windows.Markup;

namespace DependencyContentTest
{
    public interface IChangeNotifer {
        /// <summary>Dispatched when this object was modified.</summary>
        event Action<object> MODIFIED;
    }

    /// <summary>This element tracks nested <see cref="IChangeNotifer"/> descendant objects (in logical tree) of this object's parent element and resets a child in it's panel property.
    /// Only static (XAML) objects are supported i.e. object added to the tree dynamically at runtime will not be tracked.</summary>
    public class UIReseter : UIElement {

        public int searchDepth { get; set; } = int.MaxValue;

        protected override void OnVisualParentChanged(DO oldParent){
            if (VisualParent is FE p) p.Loaded += (s, e)  =>  bind(p);
        }

        private void bind(FE parent, int dl = 0) {
            if (parent == null || dl > searchDepth) return;
            var chs = H.GetChildren(parent);
            foreach (object ch in chs) {
                if (ch is UIReseter r && r != this) throw new Exception($@"There's overlapping ""{nameof(UIReseter)}"" instance in the tree. Use single global instance of check ""{nameof(UIReseter.searchDepth)}"" levels.");
                if (ch is IChangeNotifer sc) trackObject(sc, parent);
                else bind(ch as FE, ++dl);
            }
        }

        private Dictionary<IChangeNotifer, Reseter> tracked = new Dictionary<IChangeNotifer, Reseter>();
        private void trackObject(IChangeNotifer sc, FE parent) {
            var cp = getContentProperty(parent);
            if (cp == null) return;
            var r = tracked.nev(sc, () => new Reseter {
                child = sc,
                parent = parent,
                content = cp,
            });
            r.track();
        }

        private PropertyInfo getContentProperty(FE parent) {
            var pt = parent.GetType();
            var cp = parent.GetType().GetProperties(
                BindingFlags.Public |
                BindingFlags.Instance
            ).FirstOrDefault(i => Attribute.IsDefined(i,
                typeof(ContentPropertyAttribute)));
            return cp ?? pt.GetProperty("Content");
        }

        private class Reseter {
            public DO parent;
            public IChangeNotifer child;
            public PropertyInfo content;
            private bool isTracking = false;

            /// <summary>Function called by <see cref="IChangeNotifer"/> on <see cref="IChangeNotifer.MODIFIED"/> event.</summary>
            /// <param name="ch"></param>
            public void reset(object ch) {
                if(! isChildOf(child, parent)) return;
                //TODO: Handle multi-child parents
                content.SetValue(parent, null);
                content.SetValue(parent, child);
            }

            public void track() {
                if (isTracking) return;
                child.MODIFIED += reset;
            }

            private bool isChildOf(IChangeNotifer ch, DO p) {
                if(ch is DO dch) {
                    if (H.GetParent(dch) == p) return true;
                    child.MODIFIED -= reset; isTracking = false;
                    return false;
                }
                var chs = H.GetChildren(p);
                foreach (var c in chs) if (c == ch) return true;
                child.MODIFIED -= reset; isTracking = false;
                return false;
            }
        }
    }

    public static class DictionaryExtension {
        public static V nev<K,V>(this Dictionary<K,V> d, K k, Func<V> c) {
            if (d.ContainsKey(k)) return d[k];
            var v = c(); d.Add(k, v); return v;
        }
    }
    
}

它可以改进并且没有经过全面测试,但它适用于当前目的。 另一个问题是一些元素,如 TextBox 哭泣不支持 SomeContent,就像它很难使用 ToString()... 但那是另一个故事,与我的问题无关.