简体   繁体   English

依赖项属性绑定-分配新值

[英]Dependency Property Binding - Assign a new value

I've implemented a simple DependencyProperty on my WPF control to use it in Binding. 我已经在WPF控件上实现了一个简单的DependencyProperty,以便在Binding中使用它。

public static readonly DependencyProperty PollingProperty = DependencyProperty.Register("Polling", typeof(Polling), typeof(ConverterView), new UIPropertyMetadata(null));

public Polling Polling
{
    get { return (Polling)GetValue(PollingProperty); }
    set { SetValue(PollingProperty, value); }
}

The control itself is set as DataContext, so in XAML I just use it like this: 控件本身设置为DataContext,因此在XAML中,我只是这样使用它:

<ProgressBar Height="25" Value="{Binding Path=Polling.Progress, Mode=OneWay}" />

Polling.Progress is an integer property that implements INoftiyPropertyChanged, so changes get promoted to the UI. Polling.Progress是实现INoftiyPropertyChanged的整数属性,因此更改会提升到UI。 Everything works fine and changes on "Progress" are shown in the ProgressBar as expected. 一切正常,“进度”上的更改按预期显示在进度栏中。

However there is a certain point in my application where a new "Polling"-instance is applied to the DependencyProperty. 但是,在我的应用程序中有一点需要将新的“轮询”实例应用于DependencyProperty。

Polling = new Polling(); Polling.Start();

After that, the binding is no longer evaluated and the ProgressBar stays at the last value of the old instance. 之后,不再评估绑定,并且ProgressBar保持在旧实例的最后一个值。

Update: 更新:

As my case is maybe a little bit specific I'll explain a bit more. 由于我的情况可能有点具体,我将进一步解释。

A Button on my ConverterView WPF-Control allows the user to start an operation: 我的ConverterView WPF控件上的一个按钮允许用户开始操作:

private void cmdAusformatieren_Click(object sender, RoutedEventArgs e)
{
    Polling = Document.Converter(ConvertFinished);
}

The Convert-method takes a delegate as parameter, which will be invoked after the operation is finished. 转换方法将委托作为参数,将在操作完成后调用该委托。 (The whole process runs asynchronous.) The Convert-Method returns a Polling -object which provides an integer property Progress, that provides the information I want to display in a ProgressBar. (整个过程以异步方式运行。)Convert-Method返回一个轮询对象 ,该对象提供了整数属性Progress,该属性提供了我要在ProgressBar中显示的信息。 (What may be misleading here, Polling is the name of my DependencyProperty as well as the name of my class). (在这里可能会引起误解,轮询是我的DependencyProperty的名称以及我的班级的名称)。

As far as this, everything seems ok and the Binding to Polling.Progress works. 就此而言,一切似乎正常,并且对Polling.Progress的绑定有效。

In my event ConvertFinished() which triggers after the first operation is done, I get a new Polling instance returned and want to use this in Binding from then on. 在第一个操作完成后触发的事件ConvertFinished()中,我返回了一个新的轮询实例,并希望从此以后在Binding中使用它。

private void ConvertFinished(object result)
{           
    Polling = Document.Format((byte[])result, FormatFinished);
}

After this assignment to my Polling DependencyProperty, the Binding is no longer updated and stays with its previous value. 分配给我的Polling DependencyProperty后,绑定不再更新,并保持其先前的值。

Instead of 代替

Polling = new Polling () ;

you need to write 你需要写

SetCurrentValue (PollingProperty, new Polling ()) ;

When you assign a depencency property directly, all bindings on it are lost. 直接分配依赖属性时,该属性上的所有绑定都将丢失。 OTOH SetCurrentValue does not affect bindings, it just sets the current value (duh) and propagates notifications. OTOH SetCurrentValue不会影响绑定,它只是设置当前值(duh)并传播通知。

The simpliest solution, in this case, is reassign a binding , at this time from the code at runtime. 在这种情况下,最简单的解决方案是此时在运行时从代码重新分配一个binding Cause, as you noted too, the binding holds a reference to the object it was first assigned. 正如您也指出的,原因是绑定包含对它最初分配的对象的引用

Ok, could you try this: 好的,您可以尝试以下方法:

BindingOperations.ClearBinding(this.YouProgressBarName, ProgressBar.ValueProperty); BindingOperations.ClearBinding(this.YouProgressBarName,ProgressBar.ValueProperty); BindingOperations.SetBinding(this.YouProgressBarName, ProgressBar.ValueProperty); BindingOperations.SetBinding(this.YouProgressBarName,ProgressBar.ValueProperty);

first and check if it works. 首先,检查它是否有效。 I predict it won't. 我预计不会。

There's one thing I'd suggest to change - get rid of subproperties (I understand that you use complex objects to reduce the number parameters in your dels). 我建议更改一件事-摆脱子属性(我知道您使用复杂的对象来减少dels中的数量参数)。 They've limited support by WPF framework. 他们限制了WPF框架的支持。

Another thing I'd try is forcing your Polling property value to 0 just before assigning a new value to it 我想尝试的另一件事是在将轮询属性值分配为0之前将其强制为0

private void cmdAusformatieren_Click(object sender, RoutedEventArgs e)
{
    this.Polling.Progress = 0; // see if make any difference
    this.Polling = Document.Converter(ConvertFinished);

    if (this.Polling.Progress != 0) { throw new Exception(); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM