Show / Hide Table of Contents

Class ComboBoxItemDisplayBehavior

Provides different ItemTemplates and/or an additional empty item for a System.Windows.Controls.ComboBox.

Inheritance
System.Object
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Freezable
System.Windows.Media.Animation.Animatable
Microsoft.Xaml.Behaviors.Behavior
Microsoft.Xaml.Behaviors.Behavior<System.Windows.Controls.ComboBox>
ComboBoxItemDisplayBehavior
Implements
System.Windows.Media.Animation.IAnimatable
Microsoft.Xaml.Behaviors.IAttachedObject
Microsoft.Xaml.Behaviors.Behavior<System.Windows.Controls.ComboBox>.AssociatedObject
Microsoft.Xaml.Behaviors.Behavior.CreateInstanceCore()
Microsoft.Xaml.Behaviors.Behavior.Attach(System.Windows.DependencyObject)
Microsoft.Xaml.Behaviors.Behavior.Detach()
Microsoft.Xaml.Behaviors.Behavior.AssociatedType
Microsoft.Xaml.Behaviors.Behavior.Microsoft.Xaml.Behaviors.IAttachedObject.AssociatedObject
System.Windows.Media.Animation.Animatable.ApplyAnimationClock(System.Windows.DependencyProperty, System.Windows.Media.Animation.AnimationClock)
System.Windows.Media.Animation.Animatable.ApplyAnimationClock(System.Windows.DependencyProperty, System.Windows.Media.Animation.AnimationClock, System.Windows.Media.Animation.HandoffBehavior)
System.Windows.Media.Animation.Animatable.BeginAnimation(System.Windows.DependencyProperty, System.Windows.Media.Animation.AnimationTimeline)
System.Windows.Media.Animation.Animatable.BeginAnimation(System.Windows.DependencyProperty, System.Windows.Media.Animation.AnimationTimeline, System.Windows.Media.Animation.HandoffBehavior)
System.Windows.Media.Animation.Animatable.Clone()
System.Windows.Media.Animation.Animatable.FreezeCore(System.Boolean)
System.Windows.Media.Animation.Animatable.GetAnimationBaseValue(System.Windows.DependencyProperty)
System.Windows.Media.Animation.Animatable.HasAnimatedProperties
System.Windows.Freezable.CloneCore(System.Windows.Freezable)
System.Windows.Freezable.CloneCurrentValue()
System.Windows.Freezable.CloneCurrentValueCore(System.Windows.Freezable)
System.Windows.Freezable.CreateInstance()
System.Windows.Freezable.Freeze()
System.Windows.Freezable.Freeze(System.Windows.Freezable, System.Boolean)
System.Windows.Freezable.GetAsFrozen()
System.Windows.Freezable.GetAsFrozenCore(System.Windows.Freezable)
System.Windows.Freezable.GetCurrentValueAsFrozen()
System.Windows.Freezable.GetCurrentValueAsFrozenCore(System.Windows.Freezable)
System.Windows.Freezable.OnChanged()
System.Windows.Freezable.OnFreezablePropertyChanged(System.Windows.DependencyObject, System.Windows.DependencyObject)
System.Windows.Freezable.OnFreezablePropertyChanged(System.Windows.DependencyObject, System.Windows.DependencyObject, System.Windows.DependencyProperty)
System.Windows.Freezable.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs)
System.Windows.Freezable.ReadPreamble()
System.Windows.Freezable.WritePostscript()
System.Windows.Freezable.WritePreamble()
System.Windows.Freezable.CanFreeze
System.Windows.Freezable.IsFrozen
System.Windows.Freezable.Changed
System.Windows.DependencyObject.ClearValue(System.Windows.DependencyProperty)
System.Windows.DependencyObject.ClearValue(System.Windows.DependencyPropertyKey)
System.Windows.DependencyObject.CoerceValue(System.Windows.DependencyProperty)
System.Windows.DependencyObject.Equals(System.Object)
System.Windows.DependencyObject.GetHashCode()
System.Windows.DependencyObject.GetLocalValueEnumerator()
System.Windows.DependencyObject.GetValue(System.Windows.DependencyProperty)
System.Windows.DependencyObject.InvalidateProperty(System.Windows.DependencyProperty)
System.Windows.DependencyObject.ReadLocalValue(System.Windows.DependencyProperty)
System.Windows.DependencyObject.SetCurrentValue(System.Windows.DependencyProperty, System.Object)
System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty, System.Object)
System.Windows.DependencyObject.SetValue(System.Windows.DependencyPropertyKey, System.Object)
System.Windows.DependencyObject.ShouldSerializeProperty(System.Windows.DependencyProperty)
System.Windows.DependencyObject.DependencyObjectType
System.Windows.DependencyObject.IsSealed
System.Windows.Threading.DispatcherObject.Dispatcher
System.Object.Equals(System.Object, System.Object)
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Sartorius.SAF.Presentation.Behaviors
Assembly: Sartorius.SAF.Presentation.dll
Syntax
public class ComboBoxItemDisplayBehavior : Behavior<ComboBox>, IAnimatable, IAttachedObject
Remarks

This behavior provides three additional templates for items in a System.Windows.Controls.ComboBox.

The SelectionBoxItemTemplate is used for items inside the SelectionBox of the System.Windows.Controls.ComboBox. This is the template for the currently selected item. If this is null the default System.Windows.Controls.ItemsControl.ItemTemplate is used.

The EmptyItemTemplate is used for the empty item defined by EmptyItemObject inside the dropdown. If this is null the default System.Windows.Controls.ItemsControl.ItemTemplate is used.

The SelectionBoxEmptyItemTemplate is used for the empty item defined by EmptyItemObject in the SelectionBox. Use this template for 'Please Select' messages. If this is null the SelectionBoxItemTemplate is used.

The behavior will respect an existing System.Windows.Controls.ItemsControl.ItemTemplateSelector for items in the dropdown. This existing ItemTemplateSelector will only be used for the System.Windows.Controls.ItemsControl.ItemTemplate or when other templates are not available.

If EmptyItemObject is set an additional entry that acts as null-selection is shown. This will be displayed according to the current data templates. When this additional entry is selected in the drop down it will unselect the current selection. If you set either SelectionBoxEmptyItemTemplate or SelectionBoxEmptyItemText but EmptyItemObject is null the template or the text will be displayed when currently no item is selected. If EmptyItemTemplate or EmptyItemText are set an entry that unselects the current value will be shown.

Do not use this behavior on a dropdown showing value types. The empty selection is converted to null. Your databinding will break and create undesired results.

Examples

The following example code shows a System.Windows.Controls.ComboBox with a 'Please Select message' when nothing is selected and a 'Clear Selection'-Entry in the DropDown. Items can be multiline in the dropdown and are trimmed in the SelectionBox.

 <UserControl.Resources>
   <ResourceDictionary>
<!-- This must be an object with accessible Property named 'Name' and an parameterless constructor -->
       <data:DropDownItem x:Key="EmptyItemObject">
           <data:DropDownItem.Name>Clear Selection</data:DropDownItem.Name>
       </data:DropDownItem>
   </ResourceDictionary>
</UserControl.Resources>
...

<ComboBox
    x:Name="comboBox"
    ItemsSource="{Binding Items}"
    SelectedValuePath="Value"
    SelectedValue="{Binding SelectedValue}"
    SelectedItem="{Binding SelectedItem}"
    ItemContainerStyle="{DynamicResource SAF_ComboBox_ComboBoxItem}" Style="{DynamicResource SAF_ComboBox}">
    <i:Interaction.Behaviors>
        <SAF:ComboBoxItemDisplayBehavior EmptyItemObject='{StaticResource EmptyItemObject}'>
            <SAF:ComboBoxItemDisplayBehavior.SelectionBoxItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"
                               Style="{DynamicResource SAF_TextBlock}"
                               TextWrapping="NoWrap"
                               TextTrimming="CharacterEllipsis" />
                </DataTemplate>
            </SAF:ComboBoxItemDisplayBehavior.SelectionBoxItemTemplate>
            <SAF:ComboBoxItemDisplayBehavior.SelectionBoxEmptyItemTemplate>
                <DataTemplate>
                    <TextBlock Text="Please Select"
                               Style="{DynamicResource SAF_TextBlock}"
                               Opacity="0.5"
                               TextWrapping="NoWrap"
                               TextTrimming="CharacterEllipsis" />
                </DataTemplate>
            </SAF:ComboBoxItemDisplayBehavior.SelectionBoxEmptyItemTemplate>
        </SAF:ComboBoxItemDisplayBehavior>
    </i:Interaction.Behaviors>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"
		Style="{DynamicResource SAF_TextBlock}"
		TextWrapping="Wrap" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

The following example shows how to add an empty item with default templates

<ComboBox ItemsSource="{Binding Customers}"
          SelectedItem="{Binding SelectedItem}"
          DisplayMemberPath="FirstName"
          MaxWidth="200"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
          Width="200">
    <i:Interaction.Behaviors>
        <SAF:ComboBoxItemDisplayBehavior EmptyItemText="[Unselect]"
                                          SelectionBoxEmptyItemText="Please select an item"
                                          SelectionBoxEmptyItemForeground="#DADADA" />
    </i:Interaction.Behaviors>
</ComboBox>

Fields

View Source

DisplayMemberPathProperty

Identifies the dependency property for DisplayMemberPath.

Declaration
public static readonly DependencyProperty DisplayMemberPathProperty
Field Value
Type Description
System.Windows.DependencyProperty
View Source

EmptyItemObjectProperty

Declaration
public static readonly DependencyProperty EmptyItemObjectProperty
Field Value
Type Description
System.Windows.DependencyProperty
View Source

EmptyItemTemplateProperty

Identifies the dependency property for EmptyItemTemplateProperty.

Declaration
public static readonly DependencyProperty EmptyItemTemplateProperty
Field Value
Type Description
System.Windows.DependencyProperty
View Source

EmptyItemTextProperty

Identifies the depnedency property for EmptyItemText.

Declaration
public static readonly DependencyProperty EmptyItemTextProperty
Field Value
Type Description
System.Windows.DependencyProperty
View Source

SelectionBoxEmptyItemForegroundProperty

Identifies the dependency property for SelectionBoxEmptyItemForeground.

Declaration
public static readonly DependencyProperty SelectionBoxEmptyItemForegroundProperty
Field Value
Type Description
System.Windows.DependencyProperty
View Source

SelectionBoxEmptyItemTemplateProperty

Declaration
public static readonly DependencyProperty SelectionBoxEmptyItemTemplateProperty
Field Value
Type Description
System.Windows.DependencyProperty
View Source

SelectionBoxEmptyItemTextProperty

Identifies the dependency property for SelectionBoxEmptyItemText.

Declaration
public static readonly DependencyProperty SelectionBoxEmptyItemTextProperty
Field Value
Type Description
System.Windows.DependencyProperty
View Source

SelectionBoxItemTemplateProperty

The dependency property for SelectionBoxItemTemplate.

Declaration
public static readonly DependencyProperty SelectionBoxItemTemplateProperty
Field Value
Type Description
System.Windows.DependencyProperty
View Source

SelectionBoxItemTemplateSelectorProperty

Identifies the dependency property for SelectionBoxItemTemplateSelector.

Declaration
public static readonly DependencyProperty SelectionBoxItemTemplateSelectorProperty
Field Value
Type Description
System.Windows.DependencyProperty

Properties

View Source

DisplayMemberPath

Gets or sets the member path to the text that should be displayed. You do not need to set this if System.Windows.Controls.ItemsControl.DisplayMemberPath is set on the associated combobox or if you set EmptyItemObject.

Declaration
public string DisplayMemberPath { get; set; }
Property Value
Type Description
System.String
Examples

<ComboBox x:Name="comboBox" ItemsSource="{Binding Customers}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectedValuePath="Id" SelectedValue="{Binding SelectedId}" SelectedItem="{Binding SelectedItem}" ItemContainerStyle="{DynamicResource SAF_ComboBox_ComboBoxItem}" Style="{DynamicResource SAF_ComboBox}"> <i:Interaction.Behaviors> <SAF:ComboBoxItemDisplayBehavior EmptyItemText="[Unselect]" DisplayMemberPath="FirstName"> <SAF:ComboBoxItemDisplayBehavior.SelectionBoxItemTemplate> <DataTemplate> <TextBlock Text="{Binding FirstName}" Style="{DynamicResource SAF_TextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" /> </DataTemplate> </SAF:ComboBoxItemDisplayBehavior.SelectionBoxItemTemplate> <SAF:ComboBoxItemDisplayBehavior.SelectionBoxEmptyItemTemplate> <DataTemplate> <TextBlock Text="Please Select" Style="{DynamicResource SAF_TextBlock}" Opacity="0.5" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" /> </DataTemplate> </SAF:ComboBoxItemDisplayBehavior.SelectionBoxEmptyItemTemplate> </SAF:ComboBoxItemDisplayBehavior> </i:Interaction.Behaviors> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding FirstName}" Style="{DynamicResource SAF_TextBlock}" TextWrapping="Wrap" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>

View Source

EmptyItemObject

Gets or sets the object which defines the empty item. Use this if you have to use some data-binding in templates for the empty item.

Declaration
public object EmptyItemObject { get; set; }
Property Value
Type Description
System.Object
Remarks

If you do not need advanced data-binding in templates use DisplayMemberPath, SelectionBoxEmptyItemText and EmptyItemText or use EmptyItemTemplate and SelectionBoxEmptyItemTemplate without advanced bindings.

View Source

EmptyItemTemplate

Gets or sets the System.Windows.DataTemplate that should be used for an empty item.

Declaration
public DataTemplate EmptyItemTemplate { get; set; }
Property Value
Type Description
System.Windows.DataTemplate
Remarks

When this is null the default System.Windows.Controls.ItemsControl.ItemTemplate of the associated System.Windows.Controls.ComboBox is used.

View Source

EmptyItemText

Gets or sets the text that is displayed for the EmptyItemObject in the dropdown popup.

Declaration
public string EmptyItemText { get; set; }
Property Value
Type Description
System.String
View Source

SelectionBoxEmptyItemForeground

Gets or sets the foreground color for the EmptyItemObject in the SelectionBox. This is not used when SelectionBoxEmptyItemTemplate exists and System.Windows.Controls.ItemsControl.DisplayMemberPath on the associated combobox or DisplayMemberPath are set.

Declaration
public Brush SelectionBoxEmptyItemForeground { get; set; }
Property Value
Type Description
System.Windows.Media.Brush
View Source

SelectionBoxEmptyItemTemplate

Gets or sets the System.Windows.DataTemplate that should be used for an empty item in the SelectionBox.

Declaration
public DataTemplate SelectionBoxEmptyItemTemplate { get; set; }
Property Value
Type Description
System.Windows.DataTemplate
Remarks

When this is null the SelectionBoxItemTemplate is used.

View Source

SelectionBoxEmptyItemText

Gets or sets the text that is displayed for the EmptyItemObject in the SelectionBox. This is not used when SelectionBoxEmptyItemTemplate exists and System.Windows.Controls.ItemsControl.DisplayMemberPath on the associated combobox or DisplayMemberPath are set.

Declaration
public string SelectionBoxEmptyItemText { get; set; }
Property Value
Type Description
System.String
View Source

SelectionBoxItemTemplate

Gets or sets the System.Windows.DataTemplate that should be used for the SelectionBox of the System.Windows.Controls.ComboBox

Declaration
public DataTemplate SelectionBoxItemTemplate { get; set; }
Property Value
Type Description
System.Windows.DataTemplate
Remarks

When this is null the default System.Windows.Controls.ItemsControl.ItemTemplate of the associated System.Windows.Controls.ComboBox is used.

View Source

SelectionBoxItemTemplateSelector

Gets or sets the custom logic for choosing a template used to display each item in the selection box.

Declaration
public DataTemplateSelector SelectionBoxItemTemplateSelector { get; set; }
Property Value
Type Description
System.Windows.Controls.DataTemplateSelector
Remarks

This property is ignored if SelectionBoxItemTemplate is set.

Methods

View Source

OnAttached()

Called after the behavior is attached to an AssociatedObject.

Declaration
protected override void OnAttached()
Overrides
Microsoft.Xaml.Behaviors.Behavior.OnAttached()
Remarks

Override this to hook up functionality to the AssociatedObject.

View Source

OnDetaching()

Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.

Declaration
protected override void OnDetaching()
Overrides
Microsoft.Xaml.Behaviors.Behavior.OnDetaching()
Remarks

Override this to unhook functionality from the AssociatedObject.

Implements

System.Windows.Media.Animation.IAnimatable
Microsoft.Xaml.Behaviors.IAttachedObject

Extension Methods

CollectionExtensions.AddRange<T, TCollection>(TCollection, IEnumerable<T>)
SerializableObjectCloneExtension.Clone<T>(T)
DependencyObjectExtensions.GetContainer(DependencyObject)
DependencyObjectExtensions.SetContainer(DependencyObject, CompositionContainer)
DependencyObjectExtensions.FindAncestor<T>(DependencyObject)
DependencyObjectExtensions.FindFirstChildByType<T>(DependencyObject)
DependencyObjectExtensions.FindLogicalAncestor<T>(DependencyObject)
DependencyObjectExtensions.GetChildsByType<T>(DependencyObject, List<T>)
DependencyObjectExtensions.GetChildrenByType<T>(DependencyObject)
DependencyObjectExtensions.XamlClone<T>(T)
  • View Source
Back to top Generated by DocFX