简体   繁体   English

我是否可以更改依赖项属性,作为在C#/ Silverlight中更新该依赖项属性的绑定的源对象的直接结果?

[英]Can I change a dependency property as a direct result of updating the source object of the binding of that dependency property in C#/Silverlight?

I have a custom datepicker element in C#/Silverlight with a datetime property that is bound to a source property. 我在C#/ Silverlight中有一个自定义datepicker元素,其datetime属性绑定到source属性。 When I change date through the datepicker, it updates the source property correctly, but occasionally, I need to set the source property immediately back to its previous value due to it being an invalid date and have that change reflect back to the UI. 当我通过日期选择器更改日期时,它会正确更新source属性,但有时,由于它是无效的日期,因此我需要立即将source属性设置回其先前的值,并使更改反映回UI。 I am able to set the source object correctly, and then reset it correctly, but I cannot manage to change the datepicker back to its original date. 我能够正确设置源对象,然后正确地对其进行重置,但是我无法设法将日期选择器更改回其原始日期。 Note I don't mean an invalid date in terms of the format being invalid, just basically that the date is already "taken". 请注意,我并不是说格式无效的无效日期,而是基本上该日期已被“采用”。

I believe the problem is that I'm trying to set the dependency object on the custom datepicker while it's in the middle of firing its binding from the last set action. 我认为问题在于我正在尝试在自定义datepicker上设置依赖项对象,而该对象正处于从上一个set操作中触发其绑定的过程中。 To clarify: the datepicker is changed in the UI, which updates the dependency object in the custom datepicker, which fires a binding, which updates the source property, which causes the source property to set itself back to its previous value, which fails to set the dependency object again due to it still being in the middle of being set to the value the user picked in the UI. 需要说明的是:在用户界面中更改了日期选择器,这将更新自定义日期选择器中的依赖项对象,这将触发绑定,并更新源属性,这将导致源属性将自身设置回其先前的值,从而无法设置依赖项对象再次出现,因为它仍然位于用户在UI中选择的值的中间。

Can a dependency object ever be set to something new as part of the chain of events caused by setting it? 是否可以将依赖对象设置为新的东西,并将其作为由设置引起的一系列事件的一部分? Is there any way to either implement this correctly, or else somehow workaround this issue? 有什么方法可以正确实现此目的,或者以其他方式解决此问题? Is there any chance I'm misdiagnosing my problem? 我可能会误诊我的问题吗?

The custom datepicker element is basically a standard datepicker wrapped with a few other UI elements, but I believe I would have the same issue if it were just the standard datepicker. 自定义datepicker元素基本上是一个标准的datepicker,并包装有其他一些UI元素,但是我认为如果只是标准datepicker,我也会遇到同样的问题。

You could try using Dispatcher.BeginInvoke() to delay the execution of the code fragment that readjusts the source property: 您可以尝试使用Dispatcher.BeginInvoke()来延迟重新调整source属性的代码片段的执行:

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    sourceProperty = newValue;
});

Also, you might want to consider using the built-in capabilities of data validation instead of the abovementioned approach. 另外,您可能要考虑使用数据验证的内置功能来代替上述方法。

You can use something like this (in the view model): 您可以使用类似以下的内容(在视图模型中):

public DateTime Date
{
    get { return _date; }
    set
    {
        if (value [is _not_in_range_])
            throw new Exception("Value is not in range");
        _date = value;
    }
}

Example: http://msdn.microsoft.com/en-us/library/cc278072(VS.95).aspx 示例: http//msdn.microsoft.com/zh-cn/library/cc278072(VS.95).aspx

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

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