如何在WPF中使用Flyout?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在WPF中使用Flyout?相关的知识,希望对你有一定的参考价值。
在我的WPF程序中,我使用库MahApps.Metro。有一个按钮,按下后我想显示Flyout控件。
<Button Height="40" Width="40" HorizontalAlignment="Right" Margin="0,5,5,0"
VerticalAlignment="Top" Style="{DynamicResource MetroCircleButtonStyle}"
BorderThickness="1"
Grid.Column="1" Grid.RowSpan="2">
<controls:FlyoutsControl>
<controls:FlyoutsControl>
<controls:Flyout x:Name="yourMahAppFlyout" Header="Flyout" Position="Right" Width="200">
<TextBlock Text="Some text" />
</controls:Flyout>
</controls:FlyoutsControl>
</controls:FlyoutsControl>
</Button>
但是当我按下按钮时没有任何事情发生。无法理解,为什么?
编辑1:
XAML:
<Button x:Name="circleButtonSettings" Height="40" Width="40" HorizontalAlignment="Right" Margin="0,5,5,0"
VerticalAlignment="Top" Style="{DynamicResource MetroCircleButtonStyle}"
BorderThickness="1"
Grid.Column="1" Grid.RowSpan="2" Click="circleButtonSettings_Click">
<controls:FlyoutsControl>
<controls:Flyout x:Name="yourMahAppFlyout" Header="Flyout"
Position="Right" Width="200"
IsOpen="{Binding OpenFlyOut, Mode=TwoWay}">
<TextBlock Text="Some text " />
</controls:Flyout>
</controls:FlyoutsControl>
</Button>
C#:
public struct OpenCloseFlyOut
{
public OpenCloseFlyOut(bool _isFlyoutOpen)
{
IsFlyoutOpen = _isFlyoutOpen;
}
public bool IsFlyoutOpen { get; set; }
}
public partial class MainWindow : MetroWindow
{
OpenCloseFlyOut obj = new OpenCloseFlyOut(true);
// I call this method when press the button
private void ChangeFlyoutState()
{
obj.IsFlyoutOpen = !obj.IsFlyoutOpen;
}
}
毕竟它仍然无法正常工作..
答案
您需要添加绑定到bool属性的IsOpen属性。
<controls:FlyoutsControl>
<controls:FlyoutsControl>
<controls:Flyout x:Name="yourMahAppFlyout"
Header="Flyout" Position="Right" Width="200"
IsOpen="{Binding OpenFlyOut, Mode=TwoWay}">
<TextBlock Text="Some text" />
</controls:Flyout>
</controls:FlyoutsControl>
</controls:FlyoutsControl>
下面是一个使用代码隐藏的工作原理的示例,如上面的示例所示。
<Button x:Name="btnOpen" Content="open" Width="75" Click="btnOpen_Click" Height="20"/>
<Button x:Name="btnClose" Content="close" Width="75" Click="btnClose_Click" Height="20" Margin="221,125,221,175"/>
<Controls:FlyoutsControl>
<Controls:Flyout x:Name="flyout">
<TextBlock Text="Some Text"/>
</Controls:Flyout>
</Controls:FlyoutsControl>
public partial class MainWindow : MetroWindow
{
public MainWindow()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
flyout.IsOpen = true;
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
flyout.IsOpen = false;
}
}
以上是关于如何在WPF中使用Flyout?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UWP 中结合 Flyout 和 MenuFlyout?
如何将 App Shell 与每页不同的 Flyout 项目一起使用