WPF触发依赖项属性手动刷新



我有一个自定义的UserControl子类从RichTextBox。该类有一个依赖属性Equation,它是双向绑定的。

当用户将一个项目放到控件上时,我改变了Equation。这将正确地将更改传播到绑定的另一端,从而触发属性更改通知,但UI没有更改。如果我将绑定更改为另一个对象,然后返回,它将显示更新后的Equation。

如何在不更改绑定的情况下强制刷新?现在我设置的是Equation=null,然后返回,这是可行的,但这看起来有点粗糙。一定有更优雅的。

这里是控件的相关部分。我想要发生的是OnEquationChanged回调在我改变方程(Equation. components . add (txt))后被调用。

public class EquationTextBox : RichTextBox
{
    protected override void OnDrop(DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.StringFormat))
        {
            string str = (string)e.Data.GetData(DataFormats.StringFormat);
            EquationText txt = new EquationText(str);
            //// Preferred /////
            Equation.Components.Add(txt);
            //// HACK /////
            Equation eqn = this.Equation;
            eqn.Components.Add(txt);
            this.Equation = null;
            this.Equation = eqn;
            ///////////////
            Console.WriteLine("Dropping " + str);
        }
    }
    public Equation Equation
    {
        get { return (Equation)GetValue(EquationProperty); }
        set { SetValue(EquationProperty, value); }
    }
    private static void onEquationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        string prop = e.Property.ToString();
        EquationTextBox txtBox = d as EquationTextBox;
        if(txtBox == null || txtBox.Equation == null)
            return;
        FlowDocument doc = txtBox.Document;
        doc.Blocks.Clear();
        doc.Blocks.Add(new Paragraph(new Run(txtBox.Equation.ToString())));
    }
    public static readonly DependencyProperty EquationProperty =
        DependencyProperty.Register("Equation",
                                    typeof(Equation),
                                    typeof(EquationTextBox),
                                    new FrameworkPropertyMetadata(null,
                                                                  FrameworkPropertyMetadataOptions.AffectsRender,
                                                                  new PropertyChangedCallback(onEquationChanged)));
    private bool mIsTextChanged;
}

}

这是双向绑定另一端的属性。在上述代码中,equation_PropertyChanged事件作为equation_components . add (txt);

的结果被调用。
public Equation Equation
{
    get{ return mEquation; }
    set { mEquation = value; NotifyPropertyChanged(); }
}
private void equation_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    NotifyPropertyChanged("Equation");
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

编辑 --------------------------

根据注释,我尝试像这样使用分派器(注意,这是我第一次尝试使用分派器)

        string str = (string)e.Data.GetData(DataFormats.StringFormat);
        EquationText txt = new EquationText(str);
        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
            {
                Equation.Components.Add(txt); 
                NotifyPropertyChanged("Equation");
            }));

但仍然没有UI更新。

Edit 2 --------------------------

双向绑定在XAML

中完成
<l:EquationTextBox x:Name="ui_txtVariableEquation" Grid.Row="0" Grid.Column="2" 
                             Grid.RowSpan="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                   AllowDrop="True"
                                   Equation="{Binding SelectedVariableVM.Variable.Equation, Mode=TwoWay}">
                </l:EquationTextBox>

与Components对象(在Equation类中)相关的信息

public class Equation : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public Equation()
    {
        mComponents = new ObservableCollection<EquationComponent>();
        mComponents.CollectionChanged += new NotifyCollectionChangedEventHandler(components_CollectionChanged);
    }
    public Equation(string eqn) : this()
    {
        mComponents.Add(new EquationText(eqn));
    }
    public ObservableCollection<EquationComponent> Components
    {
        get{ return mComponents; }
        set{ mComponents = value; NotifyPropertyChanged();}
    }
    public override string ToString()
    {
        string str = "";
        for(int i=0; i<mComponents.Count; i++)
            str += mComponents[i].ToString();
        return str;
    }
    private void components_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        NotifyPropertyChanged("Components");
    }
    private ObservableCollection<EquationComponent> mComponents;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class Variable : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public Variable(string name = "var", VariableType type = VariableType.UnknownType) :
        this(name, "", 0, type)
    {
    }

    public class Variable : INotifyPropertyChanged
{
    public Variable(string name, string unit, object val, VariableType type)
    {
        mEquation = new Equation(name + " = " + val.ToString() + 
        mEquation.PropertyChanged += new PropertyChangedEventHandler(equation_PropertyChanged);
    }
    ...
    public Equation Equation
    {
        get{ return mEquation; }
        set { mEquation = value; NotifyPropertyChanged(); }
    }
    private void equation_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        NotifyPropertyChanged("Equation");
    }
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private Equation mEquation;
    ...
}

变量。当事件在Equation类

中引发时调用equation_PropertyChanged

我认为问题是绑定产生的值实际上不是改变(它仍然是相同的Equation对象)。如果DP值没有更改,则不会调用DP更改处理程序。

也许,在您的DP更改处理程序中,您应该订阅新方程的PropertyChanged事件,然后在底层属性更改时重新构建文档:

private static void onEquationChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
{
    var txtBox = d as EquationTextBox;
    if (txtBox == null)
        return;
    var oldEquation = e.OldValue as Equation;
    if (oldEquation != null)
        oldEquation.PropertyChanged -= txtBox.OnEquationPropertyChanged;
    var newEquation = e.NewValue as Equation;
    if (newEquation != null)
        newEquation.PropertyChanged += txtBox.OnEquationPropertyChanged;
    txtBox.RebuildDocument();
}
private void OnEquationPropertyChanged(object sender, EventArgs e)
{
    RebuildDocument();
}
private void RebuildDocument()
{
    FlowDocument doc = this.Document;
    doc.Blocks.Clear();
    var equation = this.Equation;
    if (equation != null)
        doc.Blocks.Add(new Paragraph(new Run(equation.ToString())));
}

最新更新