วิธีผูกกับคุณสมบัติ Metro Flyout ItemContainerStyle จากปุ่มใน RightWindowCommands

ฉันกำลังดำเนินการแก้ไขคำถามนี้เพื่อเป็นพื้นฐานสำหรับความพยายามในปัจจุบันของฉันในการจัดการ การเปิดและปิดเส้นทางบินของรถไฟฟ้าใต้ดิน ข้อแตกต่างคือฉันมี ToggleButton กำหนดไว้ใน RightWindowCommands ลักษณะการทำงานที่ต้องการคือ IsCheckedstatus ของปุ่มนี้ควรเชื่อมโยงกับคุณสมบัติ 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>

What is the right binding strategy to bind IsChecked to IsOpen?


person ket    schedule 21.11.2018    source แหล่งที่มา
comment
คุณจะระบุ Flyout ที่ซ่อนอยู่สำหรับปุ่มใดปุ่มหนึ่งได้อย่างไร   -  person mm8    schedule 22.11.2018
comment
@ mm8 ข้อดี - ฉันแก้ไขคำถามเพื่อรวม ItemTemplate ที่ข้ามไปก่อนหน้านี้ ที่นี่ SettingsFlyoutView คือการควบคุมผู้ใช้ซึ่งใช้ประโยชน์จาก SettingsFlyoutViewModel เป็นโมเดลมุมมองสำรอง ฉันยังคงประสบปัญหาในการเข้าเล่มอยู่เหมือนกัน ดูความพยายามใหม่ล่าสุดที่ด้านบนของส่วนที่แสดงความคิดเห็น ฉันพลาดอะไรไป?   -  person ket    schedule 26.11.2018


คำตอบ (1)


เอาล่ะ

<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>
person Cinchoo    schedule 22.11.2018
comment
เมื่อฉันเพิ่มรายการโฆษณา <Controls:Flyout> ฉันจะได้รับข้อยกเว้นรันไทม์ Items collection must be empty before using ItemsSource. ข้อแตกต่างอย่างหนึ่งระหว่างสถานการณ์ของฉันและข้อมูลโค้ดเว็บไซต์ Mahapps ก็คือ ตัวอย่างของพวกเขาไม่ได้ใช้ ItemContainerStyle ซึ่งฉันเชื่อว่าฉันต้องการเพื่อเชื่อมโยงกับคุณสมบัติ viewmodel ของฉันตามลิงก์ ที่ด้านบน ความคิดใด ๆ ที่จะแก้ไขข้อยกเว้น? - person ket; 26.11.2018
comment
ฉันไม่สามารถทำให้เกิดปัญหาของคุณได้ คุณสามารถโพสต์ตัวอย่างโค้ดของคุณเพื่อดูได้หรือไม่? - person Cinchoo; 26.11.2018
comment
ฉันได้วางสำเนาของโครงการเนื่องจากมีข้อยกเว้นเกิดขึ้นที่นี่: 1drv.ms/f/s!AsvM6Pyke4A6hCRyovosrs0kIKGG เนื้อหาด้านบนสามารถพบได้ใน MainWindow.xaml - person ket; 27.11.2018