简体   繁体   English

WPF绑定未正确更新

[英]WPF Binding not being updated Properly

I am having the most frustrating issue I have had in a while, I have setup as below, and recently it has stopped working on the clients computers for no reason(or no reason that I can tell note I haven't changed any code in this section for months and this has only started happening in the last week) what is happening is the the Text is not becoming visible when the user hits edit, the weird thing is is that it works 100% in debug, release (using VS2010) and having the program installed on my computer, and I am stumped I can seem to replicate at all on my own computer. 我遇到了一段时间以来最令人沮丧的问题,我进行了如下设置,最近它无缘无故地停止在客户端计算机上工作(或者没有理由告诉我我没有更改任何代码这部分已经有几个月了,这才在上周开始发生。正在发生的是,当用户单击“编辑”时,“文本”变得不可见,奇怪的是,它在调试,发布(使用VS2010)中可以100%工作并在我的计算机上安装了该程序,我很困惑,似乎可以完全在自己的计算机上进行复制。 My question is simply (albiet a little vague) can anyone point or tell me where to look it debug this? 我的问题很简单(有点模糊),谁能指出或告诉我在哪里调试它?

View Model Section (just a typical binding): 查看模型部分(仅是典型的绑定):

    private bool _editingSpecifications = false;
    public bool EditingSpecifications
    {
        get { return _editingSpecifications; }
        set
        {
            System.Windows.MessageBox.Show("In EditingSpecifications property PRE: "+value);
            if (_editingTraceSpecifications == value)
                return;
            _editingSpecifications = value;
            base.OnPropertyChanged("EditingSpecifications");
            System.Windows.MessageBox.Show("In EditingSpecifications property POST");
        }
    }

View: 视图:

  <TextBox Visibility="{Binding Path=EditingSpecifications, Converter={StaticResource BoolToVis2}}" Text="{Binding Path=Specifications, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Width="262">...</TextBox>

And Converter: 和转换器:

 public class BoolToVisibiltyConverter2 : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (targetType != typeof(Visibility))
            throw new InvalidOperationException("The target must be a Visibility");
        System.Windows.MessageBox.Show((bool)value + " :BoolToVisibiltyConverter2");
        if ((bool)value)
            return Visibility.Visible;
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((Visibility)value == Visibility.Visible)
            return true;
        return false;
    }
}   

Its a MVVM setup where the ViewModel binds to the view on demand(ie when the user wishes to edit etc), the code above is all I feel is relevant (if you need more just ask :)), as you can see I have some messagebox pop up and from that I get this scenario: 它是一个MVVM设置,其中ViewModel按需绑定到视图(即,当用户希望进行编辑时),上面的代码是我认为相关的(如果您需要更多,请问问:)),如您所见一些消息框弹出,从中我得到这种情况:
The user loads the first View hits edit messagebox pops up 用户加载第一个“查看匹配”,弹出编辑框
1) "In EditingSpecifications property PRE: "+value 1)“在EditingSpecifications属性中PRE:” + value
2) (bool)value + " :BoolToVisibiltyConverter2" 2)(布尔)值+“:BoolToVisibiltyConverter2”
3) "In EditingSpecifications property POST" 3)“在EditingSpecifications属性POST中”

Everything works as expected, however on loading a second view (note this could even be the same view just closed and reopened) 一切都按预期工作,但是在加载第二个视图时(请注意,甚至可以关闭并重新打开该视图)
1) "In EditingSpecifications property PRE: "+value 1)“在EditingSpecifications属性中PRE:” + value
2) (bool)value + " :BoolToVisibiltyConverter2" - but not this, so in otherwords the converter is not called on the second and any subsequent attempts to edit the specifications. 2)(bool)value +“:BoolToVisibiltyConverter2” -但这不是,因此换句话说,第二个以及随后的任何编辑规范的调用均不调用该转换器。
3) "In EditingSpecifications property POST" 3)“在EditingSpecifications属性POST中”

If you hit edit again on the same box you only get: 如果您在同一框上再次点击“编辑”,则只会得到:
1) "In EditingSpecifications property PRE: "+value - which is kinda expected. 1)“在EditingSpecifications属性中PRE:” + value-有点期望。

EDIT- Bit More Detail The problem being that only on first edit attempt will the Textbox become visible, and will not become visible on any subsequent requests after closing and opening the view :/ And I feel it has something to do with the binding not being updated from the ViewModel on update request but I can't replicate it. EDIT-更多细节问题是,只有在第一次尝试时,文本框才变得可见,并且在关闭和打开视图后,在随后的任何请求中都将不可见:/而且我觉得它与绑定没有关系根据更新请求从ViewModel更新,但我无法复制它。

And the only way to reset this is to close and open the program, and as I stated before I can only replicate this behaviour on the clients computer and never on my own. 重置此错误的唯一方法是关闭并打开程序,正如我之前所说,我只能在客户端计算机上复制此行为,而不能独自复制。 So could anyone please tell me or point me in any direction of where to look please. 因此,任何人都可以告诉我或将我指向任何方向。

There was a similar problem introduced in WPF 4.0 when navigating backwards to a page: the page would be displayed correctly, but the bindings wouldn't work. 向后导航到页面时,在WPF 4.0中引入了类似的问题:该页面将正确显示,但是绑定将不起作用。 The workaround was to wrap the contents of the View into a DataTemplate, so that the View would be instantiated anew every time it was displayed. 解决方法是将View的内容包装到DataTemplate中,以便View每次显示时都重新实例化。

Another thing you could try is setting the DataContext to null and then back to the ViewModel, in order to force a reset of the bindings. 您可以尝试的另一件事是将DataContext设置为null,然后返回到ViewModel,以强制重置绑定。

By the way: why are you writing your own BooleanToVisibilityConverter? 顺便说一句:为什么要编写自己的BooleanToVisibilityConverter? There is one in System.Windows.Controls ... System.Windows.Controls中有一个...

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

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