9.8 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |||||
|---|---|---|---|---|---|---|---|---|---|
|
2026-04-16T02:55:26.714339+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 13307045704efc6d |
Behaviors
Documentation: DTS.Common.Behaviors Module
1. Purpose
This module provides a collection of WPF-specific behaviors and utility attributes to extend UI control functionality and metadata handling in the DTS application. It enables declarative attachment of common UI patterns—such as text trimming on TextBox controls, paste interception with custom command execution, and bidirectional synchronization of multi-selection state between a ListBox and an external IList—without requiring code-behind. Additionally, it offers a generic attribute (StringMetaDataAttr) for attaching string-based metadata to types or enum values via reflection, and a framework (InteractivityTemplate) for packaging and reusing collections of behaviors and triggers as reusable XAML templates.
2. Public Interface
StringMetaDataAttr
public string MetaData { get; }
Immutable property storing the attached metadata string.internal StringMetaDataAttr(string attr)
Constructor (internal) to initializeMetaData.public static string GetStringMetaData(object o)
Retrieves theMetaDatastring from aStringMetaDataAttrapplied to the member (field/property) corresponding too.ToString()(e.g., for anenumvalue, looks up the enum field). Returnsnullif no attribute found or reflection fails.
TrimTextBoxBehavior
public class TrimTextBoxBehavior : Behavior<TextBox>
Attaches to aTextBoxand trims leading/trailing whitespace from itsTextproperty onLostFocus. If trimming occurs, it updates the binding source.
InteractivityTemplate
public class InteractivityTemplate : DataTemplate
A no-op subclass ofDataTemplate, used as a marker type for XAML-based interactivity templates.public class InteractivityItems : FrameworkElement
Holds collections ofBehaviors andTriggerBases. Exposes:public List<TriggerBase> Triggers { get; }
Lazy-initialized list for triggers.public List<Behavior> Behaviors { get; }
Lazy-initialized list for behaviors.public static readonly DependencyProperty TemplateProperty
Attached dependency property"Template"of typeInteractivityTemplate.public static InteractivityTemplate GetTemplate(DependencyObject obj)/SetTemplate(...)
Accessors for theTemplateattached property. When set, loads theInteractivityTemplate’s content (expected to be anInteractivityItemsinstance), and copies itsBehaviorsandTriggersinto the target object viaInteraction.GetBehaviors(obj)andInteraction.GetTriggers(obj).
TextBoxPasteBehavior
public static readonly DependencyProperty PasteCommandProperty
Attached dependency property forPasteCommand.public static ICommand GetPasteCommand(DependencyObject target)/SetPasteCommand(...)
Get/set thePasteCommandattached property.- Behavior:
WhenPasteCommandis set on aTextBox,ChannelCodeBuilder, orChannelNameBuilder(using theirMainEditBox), it:- Subscribes to
CommandManager.ExecutedEventforPastecommands. - On paste execution, retrieves clipboard text (though not used in current implementation).
- Invokes
PasteCommand.Execute(textBox)ifCanExecute(null)returnstrue. - Sets
e.Handled = trueto suppress default paste. - On error, publishes a
PageErrorEventvia Prism’sIEventAggregator.
- Subscribes to
MultiSelectionBehavior
public class MultiSelectionBehavior : Behavior<ListBox>
Synchronizes selection state between aListBox.SelectedItemsand an externalIList(typicallyObservableCollection<T>orBulkObservableCollection<T>).public IList SelectedItems { get; set; }
Dependency property backing the external list to sync with.- Key behaviors:
- On attach: Populates
ListBox.SelectedItemsfromSelectedItems. - On
SelectedItemsproperty change:- Clears
ListBox.SelectedItems, repopulates from new value. - Subscribes to
CollectionChangedon the new value andSelectionChangedon theListBox.
- Clears
- On
ListBox.SelectionChanged: UpdatesSelectedItems(external list) with added/removed items.- Uses
SelectedItemsStatus.SetUpdating(...)to suppress notifications during bulk updates. - Special handling for
BulkObservableCollection<IAnalogSensor>andBulkObservableCollection<ITestSetup>viaBulkOperations*methods to useAddRange/RemoveRangefor efficiency.
- Uses
- On
SelectedItems.CollectionChanged: UpdatesListBox.SelectedItems.
- On attach: Populates
3. Invariants
-
StringMetaDataAttr.GetStringMetaData(object o)- Only inspects the member named by
o.ToString()(e.g., forMyEnum.ValueX, looks forMyEnum.ValueXfield/property). - Returns
nullifoisnull, member lookup fails, or attribute is absent. - Does not inspect attributes on the type of
o, only its member.
- Only inspects the member named by
-
TrimTextBoxBehavior- Only trims on
LostFocus. Does not trim on input, typing, orGotFocus. - Only updates the binding source if the trimmed text differs from current text.
- Only trims on
-
InteractivityItems.Template- The
InteractivityTemplate.LoadContent()result must be anInteractivityItemsinstance (or compatible), otherwise casting fails. - Behavior/trigger addition is additive—does not clear existing behaviors/triggers on the target object.
- The
-
TextBoxPasteBehavior- Only handles
ApplicationCommands.Pasteor aRoutedUICommandnamed"Paste". - Does not inspect or modify clipboard content—only invokes the bound
PasteCommandwith theTextBoxas parameter. - Relies on
ContainerLocator.Containerbeing initialized (forIEventAggregatorresolution); failure here may cause silent logging errors.
- Only handles
-
MultiSelectionBehavior- Requires
SelectedItemsto be anIList(notIEnumerable) and ideallyINotifyCollectionChangedfor two-way sync. - Uses
SelectedItemsStatus.SetUpdating(...)to signal bulk operations—consumers must respect this flag to avoid spurious notifications. - Type-specific bulk operations (
BulkOperations*) only apply whenSelectedItemsis aBulkObservableCollection<T>withTbeingIAnalogSensororITestSetup. AddItemsandRemoveItemsmethods perform null checks; exceptions during item addition are logged viaAPILogger.Log(ex)but do not halt processing.
- Requires
4. Dependencies
Dependencies of this module:
- WPF:
System.Windows,System.Windows.Controls,System.Windows.Input,System.Windows.Interactivity(viaMicrosoft.Xaml.Behaviors). - Prism:
Prism.Ioc,Prism.Events(IEventAggregator,ContainerLocator). - Domain types:
DTS.Common.Controls.ChannelCodeBuilder,ChannelNameBuilder(forMainEditBox).DTS.Common.Interface.Sensors.SensorsList.IAnalogSensor,ITestSetup.DTS.Common.Classes.BulkObservableCollection<T>.DTS.Common.Enums,DTS.Common.Utilities.Logging.APILogger,SelectedItemsStatus.
Dependencies on this module:
- Likely used by UI layers (e.g., XAML views) to attach behaviors declaratively.
InteractivityItems.Templateis intended for use in XAMLDataTemplates to share interactivity logic across controls.
5. Gotchas
-
StringMetaDataAttr.GetStringMetaData- Misleading name: Operates on members (e.g., enum fields), not the type of the object passed. Passing an enum value works, but passing a non-enum object (e.g.,
new MyClass()) will likely fail unlesso.ToString()matches a member name. - Uses
GetMember(o.ToString()), which is case-sensitive and may fail ifToString()is overridden unexpectedly.
- Misleading name: Operates on members (e.g., enum fields), not the type of the object passed. Passing an enum value works, but passing a non-enum object (e.g.,
-
TrimTextBoxBehavior- Does not handle
TextChangedor validation—trimming happens only onLostFocus, which may cause UX surprises if binding updates are deferred.
- Does not handle
-
InteractivityItems.TemplateOnTemplateChangedassumesLoadContent()returns anInteractivityItemsinstance. If the XAML template is malformed or returns a different type, a runtime cast exception occurs.
-
TextBoxPasteBehavior- Clipboard text is retrieved (
Clipboard.GetText()) but never used—thePasteCommandreceives theTextBoxas parameter, but the command implementation must handle clipboard content itself. - Uses
ContainerLocator.Container(static), which may cause issues in test scenarios or if Prism container initialization is delayed.
- Clipboard text is retrieved (
-
MultiSelectionBehavior- Critical: The
SelectedItemslist must be mutable and supportAdd/Remove/Clear. If it’s read-only (e.g.,IListwrapping an array), runtime exceptions occur. - Bulk operations (
BulkOperations*) only trigger for exact type matches:BulkObservableCollection<IAnalogSensor>orBulkObservableCollection<ITestSetup>. Other generic instantiations (e.g.,BulkObservableCollection<DerivedSensor>) fall back to per-item operations. SelectedItemsStatus.SetUpdating(...)is used internally, but its implementation (SelectedItemsStatusclass) is not provided in the source—consumers must ensure it’s correctly implemented to avoid infinite loops or missed updates.
- Critical: The
-
General:
- All behaviors assume WPF’s
Interactionservice is available (viaMicrosoft.Xaml.Behaviors). - No explicit disposal/cleanup in
MultiSelectionBehaviorfor event handlers beyondOnDetaching—re-attaching may cause duplicate subscriptions if not handled carefully.
- All behaviors assume WPF’s