如何在特定条件下隐藏UWP弹出窗口

时间:2016-11-29 06:40:21

标签: c# .net wpf windows uwp

enter code here您好我在我的UWP应用中使用 Flyout 。在按钮上单击我显示弹出窗口中的项目列表。当我点击按钮时弹出按钮被打开。 但是这里我想在列表不为空时打开flyout。如果列表为空,我想隐藏弹出按钮。

为此,我编写了代码,但隐藏不起作用。 任何人都可以对此有所了解。

xaml代码:

 <Button TabIndex="4" Background="#212121" Name="btnCashPay" Click="btnCashPay_Tapped" HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="#212121" BorderThickness="0" Margin="0,-5,0,0" >
<Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="/Images/pay_bill(30_30).png"  Stretch="None"/>
   <Button.Flyout>
<Flyout x:Name="flyout" FlyoutPresenterStyle="{StaticResource Flyoutstyle}">

   <StackPanel>
 <TextBlock Grid.Row="0" Height="35" HorizontalAlignment="Center" Foreground="DarkTurquoise" FontWeight="SemiBold">Please Add Free Items To Cart </TextBlock>
 <Border x:Name="dgFreeItemsug" BorderThickness="0.5" Visibility="Visible" BorderBrush="LightSlateGray" Grid.Row="1" Background="White"  Height="200" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="10,-16,5,0">
 <ScrollViewer x:Name="svFreeItemSugg" HorizontalScrollBarVisibility="Hidden" Padding="0" VerticalScrollBarVisibility="Auto" VerticalAlignment="Top" HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="0" Margin="0,0,0,0">

 <controls:DataGrid x:Name="dgFreeItem" Height="200"  HorizontalAlignment="Stretch"  
    controls:DataGridExtensions.UseSingleSelectionAndDeselection="true" VerticalAlignment="Top"  RowBackgroundEvenBrush="White" RowBackgroundOddBrush="White" Margin="0,0,0,0" Navigate="dgFreeItem_Navigate">
     <controls:DataGrid.Columns>
 <controls:DataGridTextColumn x:Name="freeitemddesc" Width="1*" Binding="{Binding DealSku}">
 </controls:DataGridTextColumn>
  <controls:DataGridTextColumn x:Name="freeitemprice" Width="2*" Binding="{Binding DealDescription}">
  </controls:DataGridTextColumn>
  </controls:DataGrid.Columns>
 </controls:DataGrid>
 </ScrollViewer>
   </Border>
   </StackPanel>
 </Flyout>
 </Button.Flyout>
</Button>

xaml.cs代码:

private void btnCardPay_Tapped(object sender, RoutedEventArgs e)
{
    txtcardmessage.Text = string.Empty;
    media.Play();
    if (objfreeitemlist == null)
        btnCardPay.Flyout.Hide();
}

1 个答案:

答案 0 :(得分:2)

您可以将Flyout定义为按钮资源

<Button x:Name="MyButton" Content="Button" Tapped="Button_Tapped" >
    <Button.Resources>
        <Flyout x:Name="MyFlyout">
            ....
        </Flyout>
    </Button.Resources>
</Button>

这样你就必须自己打开Flyout,但你可以定义何时打开它。

private void Button_Tapped(object sender, TappedRoutedEventArgs e)
{
    var button = sender as Button;

    if (button != null && objfreeitemlist != null)
    {
        MyFlyout.ShowAt(button);
    }
}