如何在 WPF 中使用 Flyout?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33346753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:30:22  来源:igfitidea点击:

How to use Flyout in the WPF?

c#wpfxaml

提问by Dima Kozyr

In my WPF program I use library MahApps.Metro. There is a button, after pressing which I want to display Flyoutcontrol.

在我的 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>

But nothing happening when I press the button. Can't understand, why?

但是当我按下按钮时什么也没有发生。不明白,为什么?

Edit 1:

编辑1:

XAML:

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#:

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

After all it is still doesn't work..

毕竟还是不行..

回答by mrsargent

You need to add the IsOpen attribute that is bound to a bool property.

您需要添加绑定到 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>

Here is an example of how it works using the codebehind as your example above uses.

这是使用上面示例使用的代码隐藏如何工作的示例。

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