Class ComboBoxItemDisplayBehavior
Provides different ItemTemplates and/or an additional empty item for a System.Windows.Controls.ComboBox.
Inheritance
Implements
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.
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 SourceDisplayMemberPathProperty
Identifies the dependency property for DisplayMemberPath.
Declaration
public static readonly DependencyProperty DisplayMemberPathProperty
Field Value
Type | Description |
---|---|
System.Windows.DependencyProperty |
EmptyItemObjectProperty
Declaration
public static readonly DependencyProperty EmptyItemObjectProperty
Field Value
Type | Description |
---|---|
System.Windows.DependencyProperty |
EmptyItemTemplateProperty
Identifies the dependency property for EmptyItemTemplateProperty.
Declaration
public static readonly DependencyProperty EmptyItemTemplateProperty
Field Value
Type | Description |
---|---|
System.Windows.DependencyProperty |
EmptyItemTextProperty
Identifies the depnedency property for EmptyItemText.
Declaration
public static readonly DependencyProperty EmptyItemTextProperty
Field Value
Type | Description |
---|---|
System.Windows.DependencyProperty |
SelectionBoxEmptyItemForegroundProperty
Identifies the dependency property for SelectionBoxEmptyItemForeground.
Declaration
public static readonly DependencyProperty SelectionBoxEmptyItemForegroundProperty
Field Value
Type | Description |
---|---|
System.Windows.DependencyProperty |
SelectionBoxEmptyItemTemplateProperty
Declaration
public static readonly DependencyProperty SelectionBoxEmptyItemTemplateProperty
Field Value
Type | Description |
---|---|
System.Windows.DependencyProperty |
SelectionBoxEmptyItemTextProperty
Identifies the dependency property for SelectionBoxEmptyItemText.
Declaration
public static readonly DependencyProperty SelectionBoxEmptyItemTextProperty
Field Value
Type | Description |
---|---|
System.Windows.DependencyProperty |
SelectionBoxItemTemplateProperty
The dependency property for SelectionBoxItemTemplate.
Declaration
public static readonly DependencyProperty SelectionBoxItemTemplateProperty
Field Value
Type | Description |
---|---|
System.Windows.DependencyProperty |
SelectionBoxItemTemplateSelectorProperty
Identifies the dependency property for SelectionBoxItemTemplateSelector.
Declaration
public static readonly DependencyProperty SelectionBoxItemTemplateSelectorProperty
Field Value
Type | Description |
---|---|
System.Windows.DependencyProperty |
Properties
View SourceDisplayMemberPath
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 SourceEmptyItemObject
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.
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.
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 |
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 |
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.
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 |
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.
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 SourceOnAttached()
Called after the behavior is attached to an AssociatedObject.
Declaration
protected override void OnAttached()
Overrides
Remarks
Override this to hook up functionality to the AssociatedObject.
OnDetaching()
Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
Declaration
protected override void OnDetaching()
Overrides
Remarks
Override this to unhook functionality from the AssociatedObject.