2015-08-25 35 views
0

我正在使用mvvm。我加載一個包含兩個不同的標籤,像這樣一個內容控件的用戶控件:內容控制的內容已轉到標籤頁開關

<TabControl> 
    <TabItem Header="View"> 
     <StackPanel> 
      <Info:UserData/><!--UserData Control--> 
      <Button Content="View Entries" Command="{Binding BeginView}"/> 
     </StackPanel> 
    </TabItem > 
    <TabItem Header="Edit"> 
     <StackPanel> 
      <Info:UserData/><!--UserData Control--> 
      <Button Content="Edit Entries" Command="{Binding BeginEdit}"/> 
     </StackPanel> 
    </TabItem > 
</TabControl> 

用戶控件的樣子:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <ContentControl Grid.Row="0" Content="{Binding UserTypeInfo}"/> 
    <Info:UserDetailsArea Grid.Row="1"/> 
</Grid> 

當捏首先加載ContentControl中的內容設置爲一個圖像。根據某些操作,內容可能會更改爲數據表,視頻等。這部分工作正常。

加載時默認選項卡是第一個。如果我點擊第二個標籤,你應該看到相同的東西 - 用不同的按鈕,這是有效的。但是如果我回到第一個選項卡,Content控件是空的。

我需要做什麼以使兩個選項卡都顯示圖像?

的請求從視圖模型綁定到值:

private object userTypeInfo 
/// <summary> 
/// User Specific data 
/// </summary> 
public object UserTypeInfo 
{ 
    get 
    { 
     return userTypeInfo; 
    } 
    private set 
    { 
     UuserTypeInfo= value; 
     OnPropertyChanged("UserTypeInfo"); 
    } 
} 

編輯: 下面是一個簡化的例子,我認爲顯示了相同的問題:對於窗口

XAML代碼:

<Window x:Class="dualCC.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" 
     Loaded="Window_Loaded"> 
    <Grid> 
     <TabControl> 
      <TabItem Header="One"> 
       <StackPanel> 
        <Button Content="One" /> 
        <ContentControl Name="CCone"/> 
       </StackPanel> 
      </TabItem> 
      <TabItem Header="Two"> 
       <StackPanel> 
        <Button Content="Two" /> 
        <ContentControl Name="CCtwo"/> 
       </StackPanel> 
      </TabItem> 
     </TabControl> 
    </Grid> 
</Window> 

後面的代碼(您需要修復圖像的路徑):

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace dualCC 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 
      Uri uri = new Uri(@"C:\Image.jpg"); 
      BitmapImage temp = new BitmapImage(uri); 
      Image CurrentImage = new Image(); 
      CurrentImage.Source = temp; 

      CCone.Content = CurrentImage; 
      CCtwo.Content = CurrentImage; 
     } 
    } 
} 
+0

請給你的虛擬機。你還可以提供一個我可以看的簡單例子嗎? –

+0

帶有可重現問題的共享示例將會很好 – VMaleev

回答

1

這不是MVVM。在MVVM中,您從不像這樣直接在代碼隱藏中直接操作GUI元素。

要回答你的問題,問題是你正在創建一個Image,它實際上是一個子控件,並將它設置爲兩個單獨控件的內容。控件只能有一個父項。創建單獨的圖像,而不是和設置的BitmapImage爲源動力的每個:

private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     Uri uri = new Uri(@"C:\Image.jpg"); 
     BitmapImage temp = new BitmapImage(uri); 

     CCone.Content = new Image { Source = temp }; 
     CCtwo.Content = new Image { Source = temp }; 
    } 

或者更好的是使用正確的MVVM和數據綁定做到這一點。

+0

感謝您的回答。我以爲是這樣。請注意,在我提出一個簡化示例的問題的大約一半的時候。在我的實際代碼中,我使用了MVVM(如前半部分所述)。但我相信最終結果是一樣的,因爲有2個視圖,使用相同的視圖模型,相同的圖像在兩個視圖中,而父視圖切換到第二個視圖。 – TTalma