如何从RightWindowCommands中的按钮绑定到Metro Flyout ItemContainerStyle属性?

时间:2018-11-21 19:01:47

标签: wpf xaml mvvm mahapps.metro flyout

我正在研究this question的解决方案,以此作为我目前尝试管理Metro弹出窗口的打开和关闭的基础。区别在于我在ToggleButton中定义了一个RightWindowCommands。所需的行为是,此按钮的IsChecked状态应绑定到基础弹出菜单视图模型的IsOpen属性。

这是我尝试失败的XAML:

<controls:MetroWindow.Flyouts>
    <controls:FlyoutsControl x:Name="FlyoutsControl" >
        <controls:FlyoutsControl.ItemContainerStyle>
            <Style BasedOn="{StaticResource {x:Type controls:Flyout}}" TargetType="{x:Type controls:Flyout}">
                <Setter Property="Header" Value="{Binding Header}" />
                <Setter Property="IsOpen"  Value="{Binding IsOpen}" />
                <Setter Property="Position" Value="{Binding Position}" />
                <Setter Property="Theme" Value="Accent" />
            </Style>
        </controls:FlyoutsControl.ItemContainerStyle>
        <controls:FlyoutsControl.ItemTemplate>
            <DataTemplate DataType="{x:Type local:SettingsFlyoutViewModel}">
                <local:SettingsFlyoutView x:Name="SettingsFlyoutView"/>
            </DataTemplate>
        </controls:FlyoutsControl.ItemTemplate>
    </controls:FlyoutsControl>
</controls:MetroWindow.Flyouts>
<controls:MetroWindow.RightWindowCommands>
    <controls:WindowCommands>
<!--<ToggleButton Content="{iconPacks:FontAwesome Kind=FighterJetSolid}" Background="{DynamicResource AccentColorBrush}" ToolTip="Toggle flyout." BorderBrush="White" BorderThickness="1" IsChecked="{Binding ElementName=SettingsFlyoutView, Path=IsOpen}" Cursor="Hand"/>-->
        <!--<ToggleButton Content="{iconPacks:FontAwesome Kind=FighterJetSolid}" Background="{DynamicResource AccentColorBrush}" ToolTip="Toggle flyout." BorderBrush="White" BorderThickness="1" 
                      IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:MetroWindow}}, Path=IsOpen}"/>-->
        <!--<ToggleButton Content="{iconPacks:FontAwesome Kind=FighterJetSolid}" Background="{DynamicResource AccentColorBrush}" ToolTip="Toggle flyout." BorderBrush="White" BorderThickness="1" 
                      IsChecked="{Binding ElementName=FlyoutsControl, Path=DataContext.IsOpen}"/>-->
        <!--<ToggleButton Content="{iconPacks:FontAwesome Kind=FighterJetSolid}" Background="{DynamicResource AccentColorBrush}" ToolTip="Toggle flyout." BorderBrush="White" BorderThickness="1" 
                      IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:Flyout}}, Path=IsOpen}"/>-->
    </controls:WindowCommands>
</controls:MetroWindow.RightWindowCommands>

将IsChecked绑定到IsOpen的正确绑定策略是什么?

1 个答案:

答案 0 :(得分:0)

你在这里

<Controls:MetroWindow.Flyouts>
    <Controls:FlyoutsControl>
        <Controls:FlyoutsControl.ItemContainerStyle>
            <Style BasedOn="{StaticResource {x:Type Controls:Flyout}}" TargetType="{x:Type Controls:Flyout}">
                <Setter Property="Header" Value="{Binding Header}" />
                <Setter Property="IsOpen"  Value="{Binding IsOpen}" />
                <Setter Property="Position" Value="{Binding Position}" />
                <Setter Property="Theme" Value="Accent" />
            </Style>
        </Controls:FlyoutsControl.ItemContainerStyle>
        <Controls:Flyout x:Name="yourMahAppFlyout" Header="Flyout" Position="Right" Width="200">
            <!-- Your custom content here -->
        </Controls:Flyout>
    </Controls:FlyoutsControl>
</Controls:MetroWindow.Flyouts>
<Controls:MetroWindow.RightWindowCommands>
    <Controls:WindowCommands>
        <ToggleButton Content="{iconPacks:FontAwesome Kind=FighterJetSolid}" Background="{DynamicResource AccentColorBrush}" ToolTip="Toggle flyout." BorderBrush="White" BorderThickness="1" 
                  IsChecked="{Binding ElementName=yourMahAppFlyout, Path=IsOpen}" Cursor="Hand"/>
    </Controls:WindowCommands>
</Controls:MetroWindow.RightWindowCommands>