quadroid
quadroid

Reputation: 8950

ToolBar OverflowPanel remains open

I have a ToolBar with a ItemsTemplate which works fine until the OverflowPanel is Available. The OverflowPanel does not close if i select one of the context actions.

The Problem only occurs if the Items are added via the ItemsSource binding:

     <ToolBar ItemsSource="{Binding ContextActionViewModels}"
             Background="Transparent"
             ToolBarTray.IsLocked="True"
             FocusManager.IsFocusScope="False">
        <ToolBar.ItemTemplateSelector>
            <views:ContextActionTemplateSelector>
                <views:ContextActionTemplateSelector.SimpleContextActionDataTemplate>
                    <DataTemplate DataType="{x:Type viewModels:SimpleContextActionViewModel}">
                        <Button Name="Button"
                                Command="{Binding ActionCommand}"
                                Style="{StaticResource ToolBarButtonStyle}"
                                ToolTip="{userInterface:Translation Binding={Binding ToolTip}}">
                            <ContentControl Template="{Binding Icon,Converter={StaticResource NameToResourceConverter}}"
                                            Margin="5"
                                            VerticalAlignment="Center"
                                            HorizontalAlignment="Center" />
                        </Button>
                    </DataTemplate>
                </views:ContextActionTemplateSelector.SimpleContextActionDataTemplate>

   <!-- Multiple DataTemplates follow!-->

Why is the DataTemplate / ItemTemplteSelector not working properly. While hardcoded Buttons in XAML work properly?

I uploaded a full sample that illustrates what is not working here: Just Resize the window and try invoking one off the buttons in the OverflowPanel. While the 'ICommand' is executed properly the Popup stays open.

Upvotes: 0

Views: 139

Answers (1)

In the .NET framework source you can find the method that handles the closing behavior of OverflowPanel for ToolBar class:

private static void _OnClick(object e, RoutedEventArgs args)
{
    ToolBar toolBar = (ToolBar)e;
    ButtonBase bb = args.OriginalSource as ButtonBase;
    if (toolBar.IsOverflowOpen && bb != null && bb.Parent == toolBar)
        toolBar.Close();
}

When you define a DataTemplate and use ItemsSource property, the Parent property of the created button becomes null and the if check fails. This is the expected behavior of DataTemplate as described here:

For templates, the Parent of the template eventually will be null. To get past this point and extend into the logical tree where the template is actually applied, use TemplatedParent.

As a solution you can set the IsOverflowOpen property to false when you click any of the buttons:

<ToolBar Name="SomeToolBar" ItemsSource="{Binding Items}">
    <ToolBar.ItemTemplate>
        <DataTemplate DataType="local:ItemViewModel">
            <Button Command="{Binding Command}" Content="{Binding Name}" Click="ButtonBase_OnClick"/>
        </DataTemplate>
    </ToolBar.ItemTemplate>
</ToolBar>

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    SomeToolBar.IsOverflowOpen = false;
}

Upvotes: 1

Related Questions