Search code examples
c#wpfavalondockxceed

Bind visibility of a LayoutAnchorable to a checkable MenuItem


C#, WPF. I would like to show/hide AvalonDock panels using checkable menu items. Although I could do this using _Click events I believe it would be good practice to use binding instead and that it should be possible to achieve this entirely using XAML.

I suspect that the answer should be along the lines of this one and have based my attempt on one of the answers given there.

This code compiles and runs, but there is no link between the menu being checked and the anchorable pane being visible. The key line is this:

<MenuItem Header="Panel" Name="PanelVisible" IsCheckable="True" IsChecked="{Binding Path=testAnchorable.IsVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

How can I get this working?

<Window x:Class="TestAvalon.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:dock="http://schemas.xceed.com/wpf/xaml/avalondock"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Menu Height="18" HorizontalAlignment="Stretch" Name="menu1" VerticalAlignment="Top" Grid.Row="0">
            <MenuItem Header="View">
                <MenuItem Header="Panel" Name="PanelVisible" IsCheckable="True" IsChecked="{Binding Path=testAnchorable.IsVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                </MenuItem>
            </MenuItem>
        </Menu>

        <dock:DockingManager x:Name = "Dockman" DataContext = "{Binding DockManagerViewModel}"
            DocumentsSource = "{Binding Documents}" DockPanel.Dock = "Left" Grid.Row = "1" >

        <dock:LayoutRoot x:Name = "_layoutRoot" >
            <dock:LayoutPanel x:Name = "_layoutPanel" >
                <dock:LayoutAnchorablePane DockWidth="400">
                    <dock:LayoutAnchorable x:Name ="testAnchorable" Title = "TEST PANE" IsSelected = "True">
                        <TextBlock Name="tb" Text="*****"/>
                    </dock:LayoutAnchorable >
                </dock:LayoutAnchorablePane >
            </dock:LayoutPanel >
        </dock:LayoutRoot >
    </dock:DockingManager>
    </Grid>
</Grid>

Solution

  • This question now has a solution here:

    Binding an AvalonDock LayoutAnchorable IsVisible property

    (There are two answers given there and I think they both do the job.)