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