2012-08-08 40 views
1

當我的用戶控件(WindowScreen)第一次加載時,我的elementhost顯示正確。當我實例化usercontrol並傳入一個不同的id時,elementhost不會被更新。有沒有理由爲此或有沒有辦法解決這個問題?如何刷新winform內部的wpf elementhost

這是我的代碼。

WindowScreen.cs - WinForm的:

public partial class WindowScreen : UserControl 
{ 
    private WindowView _windowView; 
    private WindowViewModel _windowViewModel = null; 

    public WindowScreen(int id) 
    { 
     InitializeComponent(); 

     elementHost.Child = this.elementHost1; 
     _windowViewModel = new WindowViewModel(); 
     _windowView = (WindowView) this.elementHost.Child; 
     //_windowViewModel.LoadTypes(123); --- first load 
        _windowViewModel.LoadTypes(id); --- pass in parameter 
     _windowView.DataContext = _windowViewModel; 
    } 
} 

TestScreen.cs - WinForm的:

public partial class TestScreen : UserControl 
{ 
    public TestScreen() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     WindowScreen ws = new WindowScreen(298); 
    } 
} 

回答

0

當調用此:

WindowScreen ws = new WindowScreen(298); 

你正在創建一個新的WindowScreen,但從未將它設置爲在TestScreen控件中使用。你需要覆蓋當前的WindowScreen這一個 - 是這樣的:

// This needs to be the appropriate/correct variable 
this.windowScreen1 = new WindowScreen(298); 
+0

我試過你做this.windowScreen =新WindowScreen(298)說。但無濟於事。所以當我點擊Test usercontrol中的按鈕時,我將傳遞一個參數給WindowScreen usercontrol(它有一個元素主體),這是假設更新WindowScreen中的元素主機。我的元素主機(WindowView)仍然沒有更新。任何其他建議? – Calvin 2012-08-08 20:34:01