【问题标题】:WPF Custom Control rerendering on view model subproperties change视图模型子属性更改上的 WPF 自定义控件重新呈现
【发布时间】:2016-11-30 05:09:40
【问题描述】:

WPF 自定义控件能否跟踪视图模型子属性更改以自动重新呈现自身?

假设我有一个具有两个属性的模型:

public class FullName : ViewModel
{
    string _first;
    string _last;

    public string First
    {
        get { return _first; }
        set
        {
            _first = value;
            RaisePropertyChanged();
        }
    }

    public string Last
    {
        get { return _last; }
        set
        {
            _last = value;
            RaisePropertyChanged();
        }
    }
}

ViewModel 在哪里:

public abstract class ViewModel : INotifyPropertyChanged
{
    protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) =>
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) =>
        PropertyChanged?.Invoke(this, e);

    public event PropertyChangedEventHandler PropertyChanged;
}

我想在 WPF 自定义控件(AffectsRender,没有 SubPropertiesDoNotAffectRender)上有一个依赖属性来引用模型,以便控件在 FirstLast 属性更改时自动重新呈现:

public class Tag : Control
{
    public static readonly DependencyProperty FullNameProperty =
        DependencyProperty.Register("FullName", typeof(FullName), typeof(Tag),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));

    public FullName FullName
    {
        get { return (FullName)GetValue(FullNameProperty); }
        set { SetValue(FullNameProperty, value); }
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
        if (FullName == null)
            return;

        FontFamily courier = new FontFamily("Courier New");
        Typeface courierTypeface = new Typeface(courier, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
        FormattedText ft2 = new FormattedText(FullName.First + " " + FullName.Last,
                                             CultureInfo.CurrentCulture,
                                             FlowDirection.LeftToRight,
                                             courierTypeface,
                                             14.0,
                                             Brushes.Black);

        drawingContext.DrawText(ft2, new Point());
    }
}

这是用来测试它的 sn-p:

<Window x:Class="WpfApplication3.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:WpfApplication3"
    mc:Ignorable="d"
    Title="MainWindow" Height="139.9" Width="249.514">
  <StackPanel>
    <StackPanel.DataContext>
        <local:FullName>
            <local:FullName.First>John</local:FullName.First>
            <local:FullName.Last>Doe</local:FullName.Last>
        </local:FullName>
    </StackPanel.DataContext>
    <local:Tag FullName="{Binding}" Height="20"/>
    <TextBox Text="{Binding First, UpdateSourceTrigger=PropertyChanged}"/>
    <TextBox Text="{Binding Last, UpdateSourceTrigger=PropertyChanged}"/>
  </StackPanel>
</Window>

不幸的是,它不起作用 - 更改不会传播到自定义控件。能否高效完成?那么这个SubPropertiesDoNotAffectRender 是关于什么的呢?

【问题讨论】:

  • @nkoniishvt 实际上,请看一下Tag.OnRender,它绘制了FullName.First + " " + FullName.Last。我需要的是完全“所有者绘制”的图表组件(不涉及模板或继承)来显示动态可更新视图模型对象图的内容。我的印象是控制跟踪模型子属性更改而没有被SubPropertiesDoNotAffectRender 明确抑制,但它对我不起作用。

标签: c# .net wpf data-binding custom-controls


【解决方案1】:

为此,您的FullName 类必须是Freezable,您的FirstLast 属性必须是依赖属性。

你可以看看current implementationDependencyObject

internal void NotifySubPropertyChange(DependencyProperty dp)
{
    InvalidateSubProperty(dp);

    // if the target is a Freezable, call FireChanged to kick off
    // notifications to the Freezable's parent chain.
    Freezable freezable = this as Freezable;
    if (freezable != null)
    {
        freezable.FireChanged();
    }
}

此机制最初并非用于观察绑定视图模型的子属性。通过观察Freezables 的属性并触发更改其属性和子属性的适当操作,它有助于简化FrameworkElements 的测量、排列和渲染。

您可以找到一篇不错的博文 here,其中解释了 WPF 中保留的图形系统如何工作以及如何使用您感兴趣的功能。

【讨论】:

  • 当我的 none-Freezable 类型属性发生变化时,是否有官方方法可以调用 NotifySubPropertyChange?我使用了代码反射here,但它既慢又危险……
  • @DmitryNogin,AFAIK 没有办法做到这一点。要通过反射加快方法调用,您可以缓存 delegate,如 this article 中所述。但是,依赖 .NET Framework 的内部方法并不是编写可维护和可靠代码的好方法,因此我不建议这样做。
猜你喜欢
  • 1970-01-01
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 2022-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多