Problems using PopupContentTemplate

Ribbon for WPF Forum

Posted 12 years ago by Robert Croshere
Version: 11.1.0545
Platform: .NET 4.0
Environment: Windows 7 (64-bit)
Avatar
Hi, in the ribbon we have, a certain menu can be accessed from different places (including contextual tabs). As I don't want to define the menu mulitple times (due to maintenance), I decided to add the menu as a resource. I can't just add the menu as a StaticResouce, as the element can't be rooted multiple times. So I created a style as follows:

            <!-- Contents of the border menu -->
            <Style
                x:Key="borderMenuStyle"
                TargetType="ribbon:PopupButton">
                <Setter
                    Property="Command"
                    Value="NotACommand" />
                <Setter
                    Property="Label"
                    Value="{x:Static props:Resources.RibbonMenuCaptionOutline}" />
                <Setter
                    Property="ImageSourceSmall"
                    Value="../Images/FormatBorderMenu16.png" />
                <Setter
                    Property="ImageSourceLarge"
                    Value="../Images/FormatBorderMenu32.png" />
                <Setter
                    Property="PopupContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <ribbon:Menu>
                                <ribbon:ColorPickerGallery
                                    Command="{Binding ControlCommands.FormatBorderFill}"
                                    IsPreviewEnabled="True"
                                    InitialColumnCount="10"
                                    HorizontalAlignment="Center"
                                    CategorizedItemsSource="{StaticResource colorPickerColors}">
                                    <ribbon:ColorPickerGallery.CommandParameter>
                                        <self:BrushValueCommandParameter />
                                    </ribbon:ColorPickerGallery.CommandParameter>
                                </ribbon:ColorPickerGallery>
                                <ribbon:Button
                                    Command="{Binding ControlCommands.ResetBorder}" />
                                <ribbon:Separator />
                                <ribbon:Button
                                    Label="{x:Static props:Resources.RibbonButtonCaptionMoreColors}"
                                    Command="{Binding ControlCommands.FormatBorderFill}"
                                    CommandParameter="{x:Null}"
                                    ImageSourceSmall="../Images/MoreColors16.png" />
                                <ribbon:PopupButton
                                    Command="NotACommand"
                                    Label="{x:Static props:Resources.RibbonMenuCaptionWeight}"
                                    ImageSourceSmall="../Images/BorderWeightMenu16.png">
                                    <ribbon:PopupGallery
                                        Command="{Binding ControlCommands.FormatBorderWeight}"
                                        IsPreviewEnabled="True"
                                        UseSingleColumn="True"
                                        VerticalScrollBarVisibility="Auto"
                                        ItemsSource="{Binding Source={StaticResource borderWidths}}">
                                        <ribbon:PopupGallery.ItemTemplate>
                                            <DataTemplate>
                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition
                                                            Width="Auto" />
                                                        <ColumnDefinition
                                                            Width="Auto" />
                                                    </Grid.ColumnDefinitions>
                                                    <Line
                                                        Grid.Column="0"
                                                        VerticalAlignment="Center"
                                                        HorizontalAlignment="Center"
                                                        X1="0"
                                                        X2="100"
                                                        Y1="{Binding Converter={StaticResource mathConverter}, ConverterParameter=-2}"
                                                        Y2="{Binding Converter={StaticResource mathConverter}, ConverterParameter=-2}"
                                                        StrokeThickness="{Binding}"
                                                        Stroke="Black" />
                                                    <TextBlock
                                                        Grid.Column="1"
                                                        VerticalAlignment="Center"
                                                        Margin="7,3,3,3"
                                                        Text="{Binding}" />
                                                </Grid>
                                            </DataTemplate>
                                        </ribbon:PopupGallery.ItemTemplate>
                                        <ribbon:PopupGallery.CommandParameter>
                                            <self:DoubleValueCommandParameter />
                                        </ribbon:PopupGallery.CommandParameter>
                                    </ribbon:PopupGallery>
                                </ribbon:PopupButton>
                                <ribbon:PopupButton
                                    Command="NotACommand"
                                    Label="{x:Static props:Resources.RibbonMenuCaptionStyle}"
                                    ImageSourceSmall="../Images/BorderStyleMenu16.png">
                                    <ribbon:PopupGallery
                                        Command="{Binding ControlCommands.FormatBorderStyle}"
                                        IsPreviewEnabled="True"
                                        UseSingleColumn="True"
                                        VerticalScrollBarVisibility="Auto"
                                        ItemsSource="{Binding Source={StaticResource borderStyles}}">
                                        <ribbon:PopupGallery.ItemTemplate>
                                            <DataTemplate>
                                                <Grid
                                                    MinHeight="18">
                                                    <Line
                                                        Grid.Column="0"
                                                        VerticalAlignment="Center"
                                                        HorizontalAlignment="Center"
                                                        X1="0"
                                                        X2="100"
                                                        Y1="0"
                                                        Y2="0"
                                                        StrokeThickness="2"
                                                        Stroke="Black"
                                                        StrokeDashArray="{Binding Converter={StaticResource ReportBorderStyleToStrokeDashArrayConverter}}" />
                                                </Grid>
                                            </DataTemplate>
                                        </ribbon:PopupGallery.ItemTemplate>
                                        <ribbon:PopupGallery.CommandParameter>
                                            <self:ObjectValueCommandParameter />
                                        </ribbon:PopupGallery.CommandParameter>
                                    </ribbon:PopupGallery>
                                </ribbon:PopupButton>
                            </ribbon:Menu>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
As can be seen, the menu is always displayed in a popupbutton, so I'm setting the template for the popup here. This approach works, but leaves me with two problems:

1 - The first time the menu shows, all colors in the gallery are placed on a single line, thus creating an incredibly wide menu. The second time the menu is shown is appears ok.
2 - When all commands on the menu are disabled, the popupbutton itself is still enabled, even though the command is set to NotACommand.

How can I get rid of these problems?
Regards,
Robert Croshere

Comments (3)

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Robert,

For the first question, we're probably need you to email us a new simple sample project that shows the issue so that we can debug it. Please make it as simple and minimal as possible, and rename the .zip file extension so it doesn't get spam blocked. Also reference this post in your email.

For the second question, the command disabling feature doesn't work well when templates are used. What you can do instead is make a command that has a CanExecute handler which determines if the PopupButton should be enabled at any given time and set that to the PopupButton.Command. That will enable/disable the button as appropriate then.


Actipro Software Support

Posted 12 years ago by Robert Croshere
Avatar
Ok, I sent an email that reproduces the problem.
Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Robert,

Thanks for the sample. We've updated our code for the next version to fix this issue.


Actipro Software Support

The latest build of this product (v24.1.2) was released 0 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.