2012-09-07 48 views
0

我有關於DataContext更改的問題,我構建瞭解這方面的示例。 我在MainWindow上有MainUserControl。 MainUserControl由多個用戶控件組成。 其中一個這樣的用戶控件是SubUserControl1。爲什麼沒有更新內部對象的DataContext?

<Window x:Class="WpfApplicationUcBindingQuestion.MainWindow"> 
    <Grid> 
     ..... 
     <uc:MainUserControl /> 
    </Grid> 
</Window> 

<UserControl x:Class="WpfApplicationUcBindingQuestion.MainUserControl"> 
<Grid> 
..... 
    <uc:SubUserControl1 x:Name="subUserControl1" /> 
</Grid> 
</UserControl> 

而在MainWindow中,我有Info類的對象。類信息由幾個內部類組成。 其中之一是,讓我們說,SubInfo。 Info和SubInfo類都從INotifyPropertyChanged繼承。

,這是其中的代碼:

public class Info : INotifyPropertyChanged 
{ 
    private SubInfo m_subInfo = new SubInfo(); 

    public Info() 
    { 
    } 

    public SubInfo SubInfo 
    { 
     get 
     { 
      return m_subInfo; 
     } 
     set 
     { 
      m_subInfo = value; 
      OnPropertyChanged("SubInfo"); 
     }  
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

public class SubInfo: INotifyPropertyChanged 
{ 
    private string m_subString = "subStr"; 

    public SubInfo() 
    { 
    } 

    public string SubString 
    { 
     get 
     { 
      return m_subString; 
     } 
     set 
     { 
      m_subString = value; 
      OnPropertyChanged("SubString"); 
     }  
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

我想設置的DataContext爲MainUserControl是類的對象信息 和爲SubUserControl1的DataContext將Info.SubInfo。

下面的代碼說明這一點:

<UserControl x:Class="WpfApplicationUcBindingQuestion.SubUserControl1"> 
    <Grid> 
     ..... 
    <TextBox Text="{Binding Path=SubString}"/> 
    </Grid> 
</UserControl> 

public MainUserControl() 
{ 
     InitializeComponent(); 

     MainWindow mainWnd = (MainWindow)Application.Current.MainWindow; 

     Info info = mainWnd.Info; 
     this.DataContext = info; 

     this.subUserControl1.DataContext = info.SubInfo; 
} 

當新subInfo來到我更新信息對象中內部對象subInfo: (這是主窗口的功能)

private void OnUpdateData() 
{ 
    SubInfo arrivedSubInfo = new SubInfo(); 
    arrivedSubInfo.SubString = "newString"; 

    m_info.SubInfo = arrivedSubInfo; 
} 

我想看看DataUser for subUserControl1也被更改。

但它沒有發生,並且SubUserControl1中的TextBox未更新 並且不顯示「newString」。

(注:如果我寫裏面OnUpdateData()函數如下:

m_info.SubInfo.SubString = arrivedSubInfo.SubString; 

(複印件場場而不是整個對象)它的工作原理, 但我不「想複製50場... )

我哪裏錯了? 您的幫助將非常感激。

回答

3

你的問題如下:

在你的構造函數,當你這樣做:

this.DataContext = info; 

    this.subUserControl1.DataContext = info.SubInfo; 

您將設置DataContext的一次。這意味着它永遠不會改變,除非你在某處寫上subUserControl1.DataContext = someNewDataContext

,你能做些什麼來解決這個問題:

「正確的解決方案」:

用的結合。在XAML中,只寫:

<uc:SubUserControl1 x:Name="subUserControl1" DataContext="{Binding 
SubInfo, UpdateSourceTrigger=PropertyChanged}" /> 

這將工作,假定當它被設置你的SubInfo屬性的觸發事件OnPropertyChanged

「醜陋的解決方案」:

明確設置你的UserControlDataContext代碼隱藏當你需要它。再說一次,我不會建議,而且你最好先申請第一個解決方案!

+0

感謝您的快速回答。你的「適當的解決方案」的作品但是,我有一個問題:爲什麼我需要更新DataContext, – user1102760

+0

感謝您的快速回答。你的「適當的解決方案」的作品然而,我有一個問題:爲什麼我需要爲subUserControl1更新DataContext,如果我有PropertyChanged屬性爲Info和SubInfo類。我不更新綁定到Info對象的控件的DataContext,並且它們(控件)用新值更新。 – user1102760

+0

您不必「更新它」,但實際上您必須指定每次啓動「PropertyChanged」時都應更新綁定。爲此,您需要使用UpdateSourceTrigger = PropertyChanged'在Binding表達式中指定它。 「LostFocus」發生默認綁定更新,並不總是您需要的 – Damascus

相關問題