Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. DataTemplate Problem

DataTemplate Problem

Scheduled Pinned Locked Moved WPF
wpfcsswcfcomhelp
2 Posts 2 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have an app that displays 1 to 16 video player controls. The user can select the type of player (ie LeadTools, VLC) and the number of players to show. Once the type and number of players is selected, I load the players into the main window's Players collection. The ActiveX player is hosted in a PlayerHostViewl, and that is added to an ObservableCollection called 'Players' Here's the main window's XAML's resources, showing the data template for the collection:

    <Window.Resources>

    <DataTemplate DataType="{x:Type vms:PlayerHostViewModel}">
        <vws:PlayerHostView/>
    </DataTemplate>
    

    </Window.Resources>

    and the MainWindow's player collection:

    <Border Grid.Row="1"
    Grid.Column="0"
    BorderThickness="1"
    BorderBrush="SteelBlue"
    Margin="5,0,0,5">

    <ItemsControl ItemsSource="{Binding Players}"
                    Margin="2">
                
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid IsItemsHost="True"
                                Margin="2"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
                
    </ItemsControl>
    

    </Border>

    The MainWindowViewModel's LoadPlayers method:

    private void LoadPlayers()
    {
    // If a player type and count are selected...
    if (SelectedPlayerInfo != null && SelectedPlayerCount > -1)
    {
    // For each player to add...
    for (int i = 0; i < SelectedPlayerCount + 1; i++)
    {
    // Create the player ActiveX control
    var playerAssembly = (IPlayer)Activator.CreateInstance(SelectedPlayerInfo.PlayerType);

            // Create the player host
            var player = new PlayerHostViewModel(playerAssembly, $"Player {i + 1}", LiveStream, DVRStream);
         
            // Add the player host to the Players list
            Players.Add(player);
        }
    }
    

    }

    Here's the PlayerHost CTORs

    public PlayerHostViewModel()
    {
    }
    public PlayerHostViewModel(IPlayer player, string playerName, string liveStream, string dvrStream)
    {
    Player = player;
    PlayerName = playerName;
    LiveStream = LiveStream;
    DVRStream = dvrStream;
    }

    The problem is that the PlayerHost paramterized CTOR fires when this line executes, as it should:

    var player = new PlayerHostViewMo

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      I have an app that displays 1 to 16 video player controls. The user can select the type of player (ie LeadTools, VLC) and the number of players to show. Once the type and number of players is selected, I load the players into the main window's Players collection. The ActiveX player is hosted in a PlayerHostViewl, and that is added to an ObservableCollection called 'Players' Here's the main window's XAML's resources, showing the data template for the collection:

      <Window.Resources>

      <DataTemplate DataType="{x:Type vms:PlayerHostViewModel}">
          <vws:PlayerHostView/>
      </DataTemplate>
      

      </Window.Resources>

      and the MainWindow's player collection:

      <Border Grid.Row="1"
      Grid.Column="0"
      BorderThickness="1"
      BorderBrush="SteelBlue"
      Margin="5,0,0,5">

      <ItemsControl ItemsSource="{Binding Players}"
                      Margin="2">
                  
          <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                  <UniformGrid IsItemsHost="True"
                                  Margin="2"/>
              </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
                  
      </ItemsControl>
      

      </Border>

      The MainWindowViewModel's LoadPlayers method:

      private void LoadPlayers()
      {
      // If a player type and count are selected...
      if (SelectedPlayerInfo != null && SelectedPlayerCount > -1)
      {
      // For each player to add...
      for (int i = 0; i < SelectedPlayerCount + 1; i++)
      {
      // Create the player ActiveX control
      var playerAssembly = (IPlayer)Activator.CreateInstance(SelectedPlayerInfo.PlayerType);

              // Create the player host
              var player = new PlayerHostViewModel(playerAssembly, $"Player {i + 1}", LiveStream, DVRStream);
           
              // Add the player host to the Players list
              Players.Add(player);
          }
      }
      

      }

      Here's the PlayerHost CTORs

      public PlayerHostViewModel()
      {
      }
      public PlayerHostViewModel(IPlayer player, string playerName, string liveStream, string dvrStream)
      {
      Player = player;
      PlayerName = playerName;
      LiveStream = LiveStream;
      DVRStream = dvrStream;
      }

      The problem is that the PlayerHost paramterized CTOR fires when this line executes, as it should:

      var player = new PlayerHostViewMo

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      It sounds like you're creating two instances of the viewmodel. What does the PlayerHostView look like?


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups