簡體   English   中英

雙向綁定不起作用

[英]Two-Way Binding is not working

在我的Silverlight項目中,我正在創建文本框,這些文本框在運行時是雙向綁定到某些Context的數據。 在一個方向( 從源到目標 )的綁定似乎工作正常,但是在另一個方向( 從目標到源 )的綁定沒有顯示任何效果。

這是數據上下文:

public class Leg : INotifyPropertyChanged {

    private string passengers;

    public string Passengers {
        get { return passengers; }
        set {
            // here I have a breakpoint.
            passengers = value;
            FirePropertyChanged("Passengers"); 
         }
    }

    private void FirePropertyChanged (string property) {

        if (PropertyChanged != null) {

            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

然后在另一個地方,我將創建一個新的TextBox控件及其綁定:

Binding passengersBinding = new Binding();

// viewModelLeg is an instance of the class Leg from above    
passengersBinding.Source = viewModelLeg;

passengersBinding.Path = new PropertyPath("Passengers");
passengersBinding.Mode = BindingMode.TwoWay;

legItem.paxTextBox.SetBinding(TextBox.TextProperty, passengersBinding);

現在,當我更改乘客字符串的值時,綁定到它的相應文本框將正確更新其文本。 所以這里一切都很好。

但是,當我手動更改文本框的文本,然后使文本框失去焦點時,什么也沒發生-即沒有雙向綁定發生-文本框的新文本值沒有向下傳播到源!

我在旅客屬性的設置者處有一個斷點(上面標有斷點注釋)。 當我一切准備就緒時,當綁定的目標值已更改以更新源時,綁定引擎也會使用此公共設置器-因此,在這種情況下,必須命中斷點。 但是他沒有! 因此,似乎我可以用自己的文本框(按焦點播放或按Enter鍵)執行我想做的事情,但它從未更新其源代碼。

我在監督什么嗎? 在我的代碼或思想中都必須有一個大寫錯誤。.我將非常感謝任何想法...

編輯:

在下面的內容中,我嘗試演示如何創建XAML對象和DataContext對象。 因為我在運行時創建XAML控件及其綁定,所以我沒有找到很好的解決方案來很好地實現MVVM方法。 因此,我正在執行以下操作(這可能不是最好的方法):

我正在建模的情況是,我有一個(主要是)文本框組成的UserControl(稱為LegItem)。 在運行時,用戶可以根據自己的意願(一個接一個)創建盡可能多的userControl。

在我的ViewModel端,我有一個類(稱為Leg),可以用作一個LegItem的ViewModel。 因此,當我說n(XAML-)LegItems時,我也有n個Leg實例。 我將這些Leg對象存儲在一個List中。

因此,每當用戶單擊“添加新的腿”按鈕時,我都會執行以下操作:

// here we are inside the applications view in an .xaml.cs file

public void AddLeg () {

    // this is going to serve as the ViewModel for the new LegItem
    // I am about to create.
    Leg leg = viewModel.insertLeg();

    // here I am starting to create the visual LegItem. The ViewModel object
    // I have created in the previous step is getting along with.
    createLegItem(leg);
}

// the primary job here is to bind each contained textbox to its DataContext.
private LegItem createLeg (Leg viewModelLeg) {

    // create the visual leg item control element
    // which is defined as a XAML UserControl.
    LegItem legItem = new LegItem();

    Binding passengersBinding = new Binding();

    // viewModelLeg is an instance of the class Leg from above    
    passengersBinding.Source = viewModelLeg;

    passengersBinding.Path = new PropertyPath("Passengers");
    passengersBinding.Mode = BindingMode.TwoWay;

    legItem.paxTextBox.SetBinding(TextBox.TextProperty, passengersBinding);

}


// on the viewModel side there is this simple method that creates one Leg object
// for each LegItem the View is creating and stores inside a simple list.

public Leg InsertLeg () {

    Leg leg = new Leg();
    legList.add(leg)

    return leg;
}

新答案

由於您提到綁定實際上是綁定到自定義UserControl而不是TextBox ,因此建議您查看UserControl的XAML並確保其正確綁定了數據


舊答案

我對一個新的Silverlight項目進行了快速測試,並注意到啟動項目是SilverlightApplication1.Web ,而不是SilverlightApplication

這意味着在我運行項目時,setter中的斷點實際上不會被命中。 您會注意到斷點圓圈只是輪廓,而顏色並未填充。如果將鼠標懸停在其上,它將說出

斷點當前不會被命中。 沒有為該文檔加載任何符號

如果我啟動SilverlightApplication1而不是.Web版本, .Web斷點。

酒店得到改變正確不管我啟動的版本,但是如果我開始與項目中的斷點沒有擊中.Web版本。 我懷疑這是您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM