View Single Post
 
Old 12-04-2016, 10:19 AM
StealthRT's Avatar
StealthRT StealthRT is offline
Contributor
 
Join Date: Aug 2002
Posts: 785
Question WPF calling function in mainWindow from another xaml window

Hey all I have been trying to figure out how to go about calling a public function on my mainWindow xaml from another xaml window.

MainWindow:
Code:
Class MainWindow
    Inherits MetroWindow       

    Private Sub ToggleFlyout(index As Integer)
        Dim flyout = TryCast(Me.Flyouts.Items(index), Flyout)
        If flyout Is Nothing Then
            Return
        End If

        flyout.IsOpen = Not flyout.IsOpen
    End Sub

    Public Sub TopFlyoutCloseButtonOnClick(sender As Object, e As RoutedEventArgs)
        yourMahAppFlyout.IsOpen = False
    End Sub
End Class
MainWindow XAML:
Code:
<Controls:MetroWindow 
        x:Name="frmCRter" 
        x:Class="MainWindow"            
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        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:newRegisterProg"
        TextElement.FontWeight="Regular"
        TextElement.FontSize="13"
        TextOptions.TextFormattingMode="Ideal" 
        TextOptions.TextRenderingMode="Auto"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        mc:Ignorable="d" 
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        Background="{DynamicResource MaterialDesignPaper}"
        Height="768" Width="1336" ResizeMode="NoResize" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" IsWindowDraggable="False" IsTabStop="False" IsMinButtonEnabled="False" IsMaxRestoreButtonEnabled="False" IsCloseButtonEnabled="False" ShowCloseButton="False" ShowDialogsOverTitleBar="False" ShowIconOnTitleBar="False" ShowMinButton="False" ShowMaxRestoreButton="False" ShowSystemMenuOnRightClick="False" ShowTitleBar="False" TitlebarHeight="0" WindowStyle="None" Title="frmCRter">    
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="134,95,0,0" VerticalAlignment="Top" Width="75"  Style="{StaticResource AccentedSquareButtonStyle}"/>            
    </Grid>
    <Controls:MetroWindow.Flyouts>
        <Controls:FlyoutsControl x:Name="flyoutsControl">
            <Controls:Flyout AnimateOpacity="True" 
                             TitleVisibility="Collapsed" 
                             CloseButtonVisibility="Collapsed" 
                             IsModal="True" 
                             MinHeight="335"
                             Position="Bottom" 
                             Width="1336" 
                             Height="335"
                             Theme="Adapt"
                             x:Name="yourMahAppFlyout">
                <Grid>
                    <Frame x:Name="CenterFrame" 
                           VerticalAlignment="Stretch" 
                           VerticalContentAlignment="Center" 
                           HorizontalAlignment="Left" 
                           HorizontalContentAlignment="Center" 
                           Source="page_Keyboard.xaml" 
                           Background="Transparent" 
                           Width="1336" />
                </Grid>
            </Controls:Flyout>
        </Controls:FlyoutsControl>
    </Controls:MetroWindow.Flyouts>
</Controls:MetroWindow>
page_Keyboard:
Code:
Call MainWindow.TopFlyoutCloseButtonOnClick(Nothing, Nothing)
The above has an error of:
Quote:
Error BC30469 Reference to a non-shared member requires an object reference.
page_Keyboard XAML:
Code:
<Page x:Class="page_Keyboard"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:newRegisterProg"
      xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
      mc:Ignorable="d" 
      Background="Transparent"
      Height="335" 
      Width="1336"
      x:Name="kbMenu"
      Title="page_Keyboard">
    <Grid x:Name="theKBGrid">
        <Button x:Name="keyBoard_1" materialDesign:ShadowAssist.ShadowDepth="Depth1" FontSize="40" FontWeight="Bold" Style="{StaticResource MaterialDesignRaisedDarkButton}" Margin="1036,10,220,255" materialDesign:RippleAssist.Feedback="AliceBlue" Height="70" Width="80" Content="1" Tag="numpad"/>
        [more code here.......]
        <Button x:Name="keyBoard_period" materialDesign:ShadowAssist.ShadowDepth="Depth1" FontSize="40" FontWeight="Bold" Style="{StaticResource MaterialDesignRaisedDarkButton}" Margin="1226,250,30,15" materialDesign:RippleAssist.Feedback="AliceBlue" Height="70" Width="80" Content="." Tag="period"/>
        <!-- Start QWERTY Keys -->
        <Button x:Name="keyBoard_Q" materialDesign:ShadowAssist.ShadowDepth="Depth1" FontSize="40" FontWeight="Bold" Style="{StaticResource MaterialDesignRaisedDarkButton}" Margin="23,10,1233,255" materialDesign:RippleAssist.Feedback="AliceBlue" Height="70" Width="80" Content="Q" Tag="keyboard"/>
        <Button x:Name="keyBoard_W" materialDesign:ShadowAssist.ShadowDepth="Depth1" FontSize="40" FontWeight="Bold" Style="{StaticResource MaterialDesignRaisedDarkButton}" Margin="116,10,1140,255" materialDesign:RippleAssist.Feedback="AliceBlue" Height="70" Width="80" Content="W" Tag="keyboard"/>        
        <Button x:Name="keyBoard_blank1" materialDesign:ShadowAssist.ShadowDepth="Depth1" FontSize="40" FontWeight="Bold" Style="{StaticResource MaterialDesignRaisedDarkButton}" Margin="23,249,1140,16" materialDesign:RippleAssist.Feedback="AliceBlue" Height="70" Content="CLOSE" Tag="goback"/>
    </Grid>
</Page>
So how to do call TopFlyoutCloseButtonOnClick of the MainWindow from the page_Keyboard page?
Reply With Quote