Skip to content

Instantly share code, notes, and snippets.

@Perikles
Last active December 29, 2015 01:09
Show Gist options
  • Select an option

  • Save Perikles/7590743 to your computer and use it in GitHub Desktop.

Select an option

Save Perikles/7590743 to your computer and use it in GitHub Desktop.
Customizing HTML disabled attribute in WPF WebPopupMenu (Awesomium)
<Application
x:Class="MyWPFSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:awe="http://schemas.awesomium.com/winfx"
xmlns:local="clr-namespace:MyWPFSample">
<Application.Resources>
<ResourceDictionary>
<!-- MyWebMenuListItem -->
<Style x:Key="MyWebMenuListItem" TargetType="{x:Type ListBoxItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="IsSelected" Value="{Binding IsInitialSelection, Mode=OneWay}" />
<Setter Property="local:WebPopupMenuService.IsItemEnabled" Value="{Binding IsEnabled, Mode=OneWay}" />
<Setter Property="MinHeight" Value="{Binding DataContext.MenuInfo.ItemHeight, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" />
<Setter Property="BorderThickness" Value="0,1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border
x:Name="Bd"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True" />
<Condition Binding="{Binding Type}" Value="Option" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="False" />
<Condition Binding="{Binding IsInitialSelection}" Value="True" />
<Condition Binding="{Binding Type}" Value="Option" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsFocused, RelativeSource={RelativeSource Self}}" Value="True" />
<Condition Binding="{Binding Type}" Value="Option" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
</MultiDataTrigger>
<Trigger Property="local:WebPopupMenuService.IsItemEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- WebPopupMenu -->
<Style TargetType="{x:Type awe:WebPopupMenu}" BasedOn="{StaticResource {x:Type ListBox}}">
<Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource Self}}" />
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="MaxHeight" Value="{Binding MaxDropDownHeight}" />
<Setter Property="ItemsSource" Value="{Binding MenuItems}" />
<Setter Property="Padding" Value="-1" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="False" />
<Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling" />
<Setter Property="SelectionMode" Value="Extended" />
<Setter Property="ItemContainerStyle" Value="{StaticResource MyWebMenuListItem}" />
<Setter Property="Opacity" Value="{Binding PlacementTarget.Opacity}" />
<Setter Property="StaysOpen" Value="False" />
<Setter Property="FocusManager.IsFocusScope" Value="True" />
<Setter Property="DefaultGroupStyle">
<Setter.Value>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="FontSize" Value="{Binding DataContext.MenuInfo.ItemFontSize, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
<Setter Property="RenderOptions.ClearTypeHint" Value="Enabled" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<StackPanel x:Name="groupPanel">
<TextBlock
x:Name="groupLabel"
Text="{Binding Name}"
VerticalAlignment="Center"
Margin="5"
MinHeight="{Binding DataContext.MenuInfo.ItemHeight, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
FontWeight="Bold"/>
<ItemsPresenter />
</StackPanel>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Name}" Value="{x:Null}">
<Setter TargetName="groupLabel" Property="Visibility" Value="Collapsed" />
<Setter TargetName="groupPanel" Property="Margin" Value="0" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type core:WebMenuItem}">
<TextBlock
x:Name="label"
Text="{Binding Label}"
ToolTip="{Binding ToolTip}"
Margin="0,0,0,2"
VerticalAlignment="Center"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Type}" Value="Group">
<Setter TargetName="label" Property="FontWeight" Value="Bold" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.ComponentModel;
namespace MyWPFSample
{
public static class WebPopupMenuService
{
[AttachedPropertyBrowsableForType( typeof( ListBoxItem ) )]
public static bool GetIsItemEnabled( ListBoxItem element )
{
return (bool)element.GetValue( IsItemEnabledProperty );
}
[AttachedPropertyBrowsableForType( typeof( ListBoxItem ) )]
public static void SetIsItemEnabled( ListBoxItem element, bool value )
{
element.SetValue( IsItemEnabledProperty, value );
}
public static readonly DependencyProperty IsItemEnabledProperty =
DependencyProperty.RegisterAttached( "IsItemEnabled",
typeof( bool ),
typeof( WebPopupMenuService ), new PropertyMetadata( true ) );
}
}
@scotttrick
Copy link


<Application.Resources>

    <ResourceDictionary>
        <!-- MyWebMenuListItem -->
        <Style x:Key="MyWebMenuListItem" TargetType="{x:Type ListBoxItem}">
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="IsSelected" Value="{Binding IsInitialSelection, Mode=OneWay}" />
            <Setter Property="local:WebPopupMenuService.IsItemEnabled" Value="{Binding IsEnabled, Mode=OneWay}" />
            <Setter Property="MinHeight" Value="{Binding DataContext.MenuInfo.ItemHeight, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" />
            <Setter Property="BorderThickness" Value="0,1" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border                                 x:Name="Bd"                                 BorderBrush="{TemplateBinding BorderBrush}"                                 BorderThickness="{TemplateBinding BorderThickness}"                                 Background="{TemplateBinding Background}"                                 Padding="{TemplateBinding Padding}"                                SnapsToDevicePixels="true">
                            <ContentPresenter                                     HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                                     SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"                                     VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True" />
                                    <Condition Binding="{Binding Type}" Value="Option" />
                                </MultiDataTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                                <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
                            </MultiDataTrigger>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="False" />
                                    <Condition Binding="{Binding IsInitialSelection}" Value="True" />
                                    <Condition Binding="{Binding Type}" Value="Option" />
                                </MultiDataTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                            </MultiDataTrigger>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsFocused, RelativeSource={RelativeSource Self}}" Value="True" />
                                    <Condition Binding="{Binding Type}" Value="Option" />
                                </MultiDataTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                                <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
                            </MultiDataTrigger>
                            <Trigger Property="local:WebPopupMenuService.IsItemEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!-- WebPopupMenu -->
        <Style TargetType="{x:Type awe:WebPopupMenu}" BasedOn="{StaticResource {x:Type ListBox}}">
            <Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource Self}}" />
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="MaxHeight" Value="{Binding MaxDropDownHeight}" />
            <Setter Property="ItemsSource" Value="{Binding MenuItems}" />
            <Setter Property="Padding" Value="-1" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="False" />
            <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling" />
            <Setter Property="SelectionMode" Value="Extended" />
            <Setter Property="ItemContainerStyle" Value="{StaticResource MyWebMenuListItem}" />
            <Setter Property="Opacity" Value="{Binding PlacementTarget.Opacity}" />
            <Setter Property="StaysOpen" Value="False" />
            <Setter Property="FocusManager.IsFocusScope" Value="True" />
            <Setter Property="DefaultGroupStyle">
                <Setter.Value>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="FontSize" Value="{Binding DataContext.MenuInfo.ItemFontSize, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" />
                                <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
                                <Setter Property="RenderOptions.ClearTypeHint" Value="Enabled" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <StackPanel x:Name="groupPanel">
                                                <TextBlock                                                         x:Name="groupLabel"                                                         Text="{Binding Name}"                                                         VerticalAlignment="Center"                                                         Margin="5"                                                         MinHeight="{Binding DataContext.MenuInfo.ItemHeight, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"                                                         FontWeight="Bold"/>
                                                <ItemsPresenter />
                                            </StackPanel>
                                            <ControlTemplate.Triggers>
                                                <DataTrigger Binding="{Binding Name}" Value="{x:Null}">
                                                    <Setter TargetName="groupLabel" Property="Visibility" Value="Collapsed" />
                                                    <Setter TargetName="groupPanel" Property="Margin" Value="0" />
                                                </DataTrigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate DataType="{x:Type core:WebMenuItem}">
                        <TextBlock                                 x:Name="label"                                 Text="{Binding Label}"                                 ToolTip="{Binding ToolTip}"                                 Margin="0,0,0,2"                                 VerticalAlignment="Center"/>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Type}" Value="Group">
                                <Setter TargetName="label" Property="FontWeight" Value="Bold" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>




</Application.Resources>

@scotttrick
Copy link

using System;
using System.Linq;
using System.Windows;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Controls;

namespace ABContainer
{
public static class WebPopupMenuService
{

    [AttachedPropertyBrowsableForType(typeof(ListBoxItem))]
    public static bool GetIsItemEnabled(ListBoxItem element)
    {
        return (bool)element.GetValue(IsItemEnabledProperty);
    }

    [AttachedPropertyBrowsableForType(typeof(ListBoxItem))]
    public static void SetIsItemEnabled(ListBoxItem element, bool value)
    {
        element.SetValue(IsItemEnabledProperty, value);
    }


    public static readonly DependencyProperty IsItemEnabledProperty =
        DependencyProperty.RegisterAttached( "IsItemEnabled",
        typeof( bool ),
        typeof(WebPopupMenuService), new PropertyMetadata(true));
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment