Hiding flyout of a button <p>I got my <em>DataTemplate</em> for items and within this <em>DataTemplate</em> I have such code:</p> <pre><code>&lt;Button x:Name="DoneButton" Style="{StaticResource ButtonStyle1}" BorderThickness="1" Margin="0,0,20,0" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="2" Grid.Row="1" Width="50" Height="50" &gt; &lt;Image Source="Images/WPIcons/checked.png" Width="30" Height="30" Margin="-10,0,-10,0" /&gt; &lt;Button.Flyout&gt; &lt;Flyout x:Name="myFly"&gt; &lt;Grid Margin="10"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Grid.Row="0" x:Uid="myNote" Text="Note: " Style="{StaticResource myText}" /&gt; &lt;TextBox Grid.Row="1" TextWrapping="Wrap" AcceptsReturn="True" Height="40" x:Name="note" Text="{Binding RecentNote, Mode=TwoWay}" Style="{StaticResource TextBoxStyle1}"/&gt; &lt;Button x:Name="CompletedButton" Command="{Binding CompletedCommand}" CommandParameter="{Binding}" Style="{StaticResource ButtonStyle1}" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="2" Click="CompletedButton_Click" Content="Done" MinWidth="80" Height="40" /&gt; &lt;/Grid&gt; &lt;/Flyout&gt; &lt;/Button.Flyout&gt; &lt;/Button&gt; </code></pre> <p>After the flyout for the item has been called and user put his data in it I want to hide this flyout as soon as user hits the "Done" button (<strong><em>x:Name="CompletedButton"</em></strong>).</p> <p>I tried to do that in code behind like:</p> <pre><code>private void CompletedButton_Click(object sender, RoutedEventArgs e) { Button button = (Button)sender; Grid grid = (Grid)VisualTreeHelper.GetParent(button); Flyout fly = (Flyout)VisualTreeHelper.GetParent(grid); fly.Hide(); } </code></pre> <p>But I get cast exception with that I can't cast <em>ContentPresenter</em> to <em>Flyout</em> so I guess it's not the way I look for. How I can hide this flyout?</p>