如何在WPF中使用Flyout?

时间:2015-10-26 13:17:03

标签: c# wpf xaml

在我的WPF程序中,我使用库MahApps.Metro。按下后有一个按钮,我想显示Flyout控件。

enter image description here

<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;
    }
}

毕竟它仍然无法运作..

1 个答案:

答案 0 :(得分:3)

您需要添加绑定到bool属性的IsOpen属性。

<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;
    }
}
{{1}}