1

i'm beginning in C# and my problem is he following :

I'd like to use a WPF with buttons, textBoxs etc... to control (display informations and videos in) another WPF.

But I can't find how to properly interact with the other WPF.

Code of my WPF windows :

<Window x:Class="WPF_AVC.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPF_AVC"
    mc:Ignorable="d"
    Title="ConfiguratorAVC" Height="350" Width="525">
<Grid Background="Black">
    <TextBox x:Name="textBox" HorizontalAlignment="Center" Height="22" Margin="0,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Background="Black" BorderBrush="#FF707070" Foreground="#FF707071" SelectionBrush="Black" TextChanged="textBox_TextChanged" KeyDown="OnKeyDownHandler"/>
    <TextBlock x:Name="textBlock" HorizontalAlignment="Center" Margin="0,32,0,0" TextWrapping="Wrap" Text="Entrez un mot" VerticalAlignment="Top" Width="120" Foreground="#FF707071" TextAlignment="Center"/>
    <Button x:Name="generate_words_btn" Content="Generer hologramme" HorizontalAlignment="Center" Margin="0,76,0,0" VerticalAlignment="Top" Width="120" Background="Black" Foreground="#FF707070" Click="generate_words_btn_Click"/>
    <Border Margin="0,267,-0.2,22" HorizontalAlignment="Center">
        <Grid>
            <Button x:Name="Pay_btn" Content="►" HorizontalAlignment="Center" Margin="0,0,243,0" VerticalAlignment="Top" Width="74" Background="Black" FontWeight="Bold" Foreground="#FF707071" HorizontalContentAlignment="Center" Height="30" Click="Pay_btn_Click" OpacityMask="Black">
                <Button.BindingGroup>
                    <BindingGroup/>
                </Button.BindingGroup>
            </Button>
            <Button x:Name="Stop_btn" Content="■" HorizontalAlignment="Center" Margin="160,0,81,0" VerticalAlignment="Top" Width="76" Background="Black" FontWeight="Bold" Foreground="#FF707071" HorizontalContentAlignment="Center" Height="30" Click="Stop_btn_Click">
                <Button.BindingGroup>
                    <BindingGroup/>
                </Button.BindingGroup>
            </Button>
            <Button x:Name="Pause_btn" Content="❚❚" HorizontalAlignment="Center" Margin="79,0,162,0" VerticalAlignment="Center" Width="76" Background="Black" FontWeight="Bold" Foreground="#FF707071" HorizontalContentAlignment="Center" Height="30" Click="Pause_btn_Click">
                <Button.BindingGroup>
                    <BindingGroup/>
                </Button.BindingGroup>
            </Button>
            <Button x:Name="Open_btn" Content="Open" HorizontalAlignment="Center" Margin="241,0,0,0" VerticalAlignment="Top" Width="76" Background="Black" FontWeight="Bold" Foreground="#FF707071" HorizontalContentAlignment="Center" Height="30" Click="Open_btn_Click" OpacityMask="Black">
                <Button.BindingGroup>
                    <BindingGroup/>
                </Button.BindingGroup>
            </Button>
        </Grid>
    </Border>
    <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="84" Margin="53,120,0,0" VerticalAlignment="Top" Width="100" Background="Black" BorderBrush="#FF707070">
        <ListBoxItem Content="Iron Man" Foreground="#FF707071" Selected="ListBoxItem_Selected"/>
        <ListBoxItem Content="Daft Punk" Foreground="#FF707071" Selected="ListBoxItem_Selected_1"/>
        <ListBoxItem Content="Link Shield" Foreground="#FF707071" Selected="ListBoxItem_Selected_2"/>
        <ListBoxItem Content="Anonymous" Foreground="#FF707071" Selected="ListBoxItem_Selected_3"/>
    </ListBox>

</Grid>

<Window x:Name="holoAvcWindow" x:Class="WPF_AVC.DisplaynWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPF_AVC"
    mc:Ignorable="d"
    Title="HoloAVC" Height="950.333" Width="1600" Background="Black" WindowStyle="ToolWindow" SnapsToDevicePixels="True" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" >
<Window.BindingGroup>
    <BindingGroup/>
</Window.BindingGroup>
<Grid>
    <MediaElement x:Name="mediaElement" HorizontalAlignment="Center" Height="720" Margin="0" VerticalAlignment="Center" Width="1280"/>
    <Grid Margin="336,0,336,50">
        <MediaElement x:Name="videoDisplayElement" Height="300" VerticalAlignment="Top" Margin="0,311,622,0" RenderTransformOrigin="0.5,0.5" OpacityMask="Black">
            <MediaElement.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="-90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </MediaElement.RenderTransform>
        </MediaElement>
        <MediaElement x:Name="videoDisplayElement_Copy" HorizontalAlignment="Center" Height="300" Width="300" Margin="311,622,311,0" RenderTransformOrigin="0.5,0.5" OpacityMask="Black" VerticalAlignment="Top">
            <MediaElement.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="180"/>
                    <TranslateTransform/>
                </TransformGroup>
            </MediaElement.RenderTransform>
        </MediaElement>
        <MediaElement x:Name="videoDisplayElement_Copy1" HorizontalAlignment="Center" Height="300" VerticalAlignment="Top" Width="300" Margin="311,0" OpacityMask="Black"/>
        <MediaElement x:Name="videoDisplayElement_Copy2" HorizontalAlignment="Left" Height="300" VerticalAlignment="Top" Width="300" Margin="622,311,0,0" RenderTransformOrigin="0.5,0.5" OpacityMask="Black">
            <MediaElement.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="+90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </MediaElement.RenderTransform>
        </MediaElement>
        <TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Margin="60,371,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="White" FontSize="48" Width="300" Height="180" RenderTransformOrigin="0.5,0.5" TextAlignment="Center">
            <TextBlock.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="-90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </TextBlock.RenderTransform>
        </TextBlock>
    </Grid>
    <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="647,120,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="White" FontSize="48" Width="300" Height="180" TextAlignment="Center"/>
    <TextBlock x:Name="textBlock3" HorizontalAlignment="Left" Margin="898,371,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="White" FontSize="48" Width="300" Height="180" RenderTransformOrigin="0.5,0.5" TextAlignment="Center">
        <TextBlock.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="90"/>
                <TranslateTransform/>
            </TransformGroup>
        </TextBlock.RenderTransform>
    </TextBlock>
    <TextBlock x:Name="textBlock4" HorizontalAlignment="Left" Margin="647,622,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="White" FontSize="48" Width="300" Height="180
               " RenderTransformOrigin="0.5,0.5" TextAlignment="Center">
        <TextBlock.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="180"/>
                <TranslateTransform/>
            </TransformGroup>
        </TextBlock.RenderTransform>
    </TextBlock>
</Grid>

I know that the codes above are not optimised.

I'd like to understand how from an input in the first WPF you can display a video in the second WPF for example.

Thank you !

Cédric MEYER
  • 460
  • 1
  • 7
  • 21
  • Is there any particular reason to use separate windows rather than have the controls on the same window as the display. In any case - here is some help on starting a second wpf window from the first - http://stackoverflow.com/questions/11133947/how-to-open-second-window-from-first-window-in-wpf - as long as your controls in the second window are public than you can access them via the variable. – PaulF Jan 12 '17 at 13:39
  • The reason is to learn how to make them communicate. A control pannel and a display window (control pannel has to stay open and make dynamic changes to the viewpanel). I've seen some talking about MVVM so i'm looking in this direction, but i can hardly see how to adapt it to my problem. – Cédric MEYER Jan 12 '17 at 14:10

1 Answers1

0

There's plenty you can do. If the "play" button is to open the second wpf you can add the click event to the Play_btn:

private void Play_btn_Click(object sender, RoutedEventArgs e)
{
holoAvcWindow avc = new holoAvcWindow();
avc.Show();
this.Close();
}

Or you can do .ShowDialog(); and do not write the this.Close(); if you want both open, but the user won't be able to use the first screen unless he closes the second one. Or just delete the this.Close(); and keep the .Show();. This way the user will be able to use both at the same time.

So we have:

  • .Show() and this.Close(): will close the first screen and show the holoAcvWindow.

  • .Show() without this.Close() NOT RECOMMENDED: will keep both open and the user will be able to use both. It's not the best choise because, if there's a button that opens the second window, the user can open 1000 windows if he wants.

  • .ShowDialog(): will open the second holoAvcWindow. Will keep the MainWindow open, but the user won't be able to user unless holoAvcWindow is closed.

Also, if there's anything you need the first screen to send foward to the second one, a url for example, just create an inicialized method that requires a parameter. Like:

public holoAvcWindow(string url)
{
    InitializeComponent();
}

and call the second screen like this:

private void Play_btn_Click(object sender, RoutedEventArgs e)
{
      holoAvcWindow avc = new holoAvcWindow(url);
      avc.Show();
      this.Close();
}

If you want both screens opened when the program is loaded, add this to initialize method of the main window:

      holoAvcWindow avc = new holoAvcWindow(url);
      avc.Show();

Should look like this:

public MainWindow()
{
    InitializeComponent();
    holoAvcWindow avc = new holoAvcWindow();
    avc.Show();
}
  • I would like to have both windows open when I start the program. Play Pause and Stop buttons are going to be used just to control videos. The list will select a video and the play button will start the video. Text bo is going to display text into a textbloc int othe Display Window. I'd like to "refresh" / change both text and videos from the control WPF window. – Cédric MEYER Jan 12 '17 at 14:38
  • Answer updated with how to have both windows when program initializes. –  Jan 12 '17 at 14:41
  • Remmeber to mark the answer as the right one if it works, and rate it! ^^ –  Jan 12 '17 at 14:54
  • Note that either window can be closed independently - so if you use the latter method of opening the 2nd window in the constructor of the 1st window then it would be a good idea that you close the 2nd window when you close the first. It would also be a good idea to check if the 2nd window gets closed by adding an event to its Closing event - otherwise you will get errors if you continue to try to use it. – PaulF Jan 12 '17 at 17:09