DataTemplate Problem
-
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
-
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
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