2013-03-13 70 views
0

我有TabControl,我想在TabItems的內容中有視頻。 我有方法的視頻添加到選項卡的內容:WPF TabControl當更改選定項MediaUriElement隱藏

private void AddVideosFromListToTab(IEnumerable<VideoClass> list, TabItem tab) 
    { 
     var stackPanel = new StackPanel { Orientation = Orientation.Horizontal }; 
     foreach (
      var media in 
       list.Select(
        video => 
        MediaUriElementHelper.CreatePreviewMediaElementUri(video.SamplePath(), 200, 150, mediaPreviewControls, 
                     mediaPlayer_MouseDown))) 
     { 
      stackPanel.Children.Add(media); 
     } 
     stackPanel.Children.Add(new TextBlock {Text = "Some text"}); 
     tab.Content = stackPanel; 
    } 

這是我與影像製作媒體的方法。我使用MediaUriElement,因爲它在性能上更好。

public static MediaUriElement CreatePreviewMediaElementUri(string sourceFile, int width, int height, List<MediaUriElement> listMedias, System.Windows.Input.MouseButtonEventHandler mouseDown) 
    { 
     var media = new MediaUriElement(); 
     media.BeginInit(); 
     media.Width = width; 
     media.Height = height; 
     media.Margin = new Thickness(20); 
     media.Volume = 0; 
     media.LoadedBehavior = WPFMediaKit.DirectShow.MediaPlayers.MediaState.Play; 
     media.MouseDown += mouseDown; 
     media.Loop = true; 
     media.Source = new Uri(sourceFile); 
     media.EndInit(); 
     listMedias.Add(media); 
     return media; 
    } 

當應用程序加載它的好,但是當我選擇另一個選項卡中有沒有視頻,當我再回到第一個選項卡有沒有太多了。我添加了一些文字,並且總是存在。所以問題出在MediaUriElement上。有人知道如何解決這個問題嗎?謝謝

回答

0

我設法解決它,而不使用TabControlEx(因爲它沒有爲我工作)。 我已經使用Datatemplates和templateselector爲了切換標籤。

的Xaml:

<Window.Resources> 
    <local:MainTabViewDataTemplateSelector x:Key="myMainContentTemplateSelector" /> 
    <DataTemplate x:Key="Dashboard"> 
     <views:DashboardView /> 
    </DataTemplate> 
    <DataTemplate x:Key="SystemHealth"> 
     <views:SystemHealthView /> 
    </DataTemplate> 
</Window.Resources> 
     <TabControl ItemsSource="{Binding MainTabs}" 
       Margin="0,33,0,0" 
       Grid.RowSpan="2" 
       SelectedIndex="0" 
       Width="auto" 
       Style="{DynamicResource TabControlStyleMain}" 
       ContentTemplateSelector="{StaticResource myMainContentTemplateSelector}" 
       Padding="20" Grid.ColumnSpan="2" 
       VerticalAlignment="Stretch"> 
     <TabControl.Background> 

      <ImageBrush ImageSource="/SystemHealthAndDashboard;component/Images/innerBackground.png"/> 

      </TabControl.Background> 
     <TabControl.ItemTemplate> 
      <DataTemplate > 
        <TextBlock Grid.Column="0" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left"/> 
      </DataTemplate> 
     </TabControl.ItemTemplate> 
    </TabControl> 

的DataTemplateSelector:

public class MainTabViewDataTemplateSelector : DataTemplateSelector 
{ 
    public override DataTemplate SelectTemplate(object item, DependencyObject container) 
    { 
     FrameworkElement element = container as FrameworkElement; 
     switch ((item as TabInfoEntity).TabIndex) 
     { 
      case 1: 
       { 
        return element.FindResource("Dashboard") as DataTemplate; 
       } 
      case 2: 
       { 
        return element.FindResource("SystemHealth") as DataTemplate; 
       } 

     } 
     return null; 
    } 
} 

TabInfoEntity類(此類型的對象的列表是的TabControl的的ItemSource):

公共類TabInfoEntity { 公共TabInfoEntity() {

} 
    private string name; 

    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 

    private int tabindex; 

    public int TabIndex 
    { 
     get { return tabindex; } 
     set { tabindex = value; } 
    } 
} 
相關問題