LiteObservableBehaviors 1.0.0

dotnet add package LiteObservableBehaviors --version 1.0.0
                    
NuGet\Install-Package LiteObservableBehaviors -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LiteObservableBehaviors" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LiteObservableBehaviors" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="LiteObservableBehaviors" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LiteObservableBehaviors --version 1.0.0
                    
#r "nuget: LiteObservableBehaviors, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LiteObservableBehaviors@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LiteObservableBehaviors&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=LiteObservableBehaviors&version=1.0.0
                    
Install as a Cake Tool

LiteObservableBehaviors

Lite version of WPF behaviors with fewer features but better performance and a smaller surface area.

Features

  • Common WPF Behavior / TriggerAction implementations for MVVM bindings.
  • Lightweight attached behavior patterns.
  • Markup extensions for event-to-command bindings.
  • XAML namespace: xmlns:lob="http://schemas.github.com/liteobservablebehaviors/2026/xaml".

Usage

Declare the library namespace and Microsoft.Xaml.Behaviors interactivity namespace in your XAML:

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:b="http://schemas.github.com/liteobservablebehaviors/2026/xaml"

Background: why these two types exist

WPF TextBox exposes properties such as SelectionStart, SelectionLength, SelectedText, and CaretIndex, but they are not dependency properties, so they cannot be bound to a ViewModel through ordinary bindings.

LiteObservableBehaviors addresses this with two complementary types:

Type Direction Role
TextBoxSetStateToControlBehavior Source → Control Push ViewModel values into the TextBox
TextBoxSetStateToSourceAction Control → Source Push TextBox state back to the ViewModel

Each instance binds a single property via its Property and Source members.

TextBoxSetStateToControlBehavior

One-way binding from ViewModel to TextBox

When a ViewModel property changes, this behavior writes the new value onto a non-bindable TextBox property such as SelectionStart, SelectionLength, SelectedText, or CaretIndex. Attach one instance per property inside Interaction.Behaviors.

XAML example
<TextBox>
    <i:Interaction.Behaviors>
        <b:TextBoxSetStateToControlBehavior
            Property="SelectionStart"
            Source="{Binding CaretPosition}" />
        <b:TextBoxSetStateToControlBehavior
            Property="SelectionLength"
            Source="{Binding SelectionLen}" />
    </i:Interaction.Behaviors>
</TextBox>
Behavior

When ViewModel properties such as CaretPosition or SelectionLen raise change notifications (INotifyPropertyChanged), the corresponding TextBox properties are updated automatically.

TextBoxSetStateToSourceAction

One-way push from TextBox to ViewModel

When the user interacts with the TextBox (for example, by changing the selection), this trigger action reads the current control state and writes it to the ViewModel. Place it inside Interaction.Triggers and invoke it from the appropriate event.

XAML example
<TextBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <b:TextBoxSetStateToSourceAction
                Property="SelectionStart"
                Source="{Binding CaretPosition, Mode=OneWayToSource}" />
            <b:TextBoxSetStateToSourceAction
                Property="SelectionLength"
                Source="{Binding SelectionLen, Mode=OneWayToSource}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>
Behavior

Each time SelectionChanged fires, the current SelectionStart and SelectionLength values are written to the bound ViewModel properties.

Combined example (near two-way sync)

Use both types together to approximate two-way synchronization:

<TextBox>

    
    <i:Interaction.Behaviors>
        <b:TextBoxSetStateToControlBehavior
            Property="SelectionStart"
            Source="{Binding CursorPos}" />
        <b:TextBoxSetStateToControlBehavior
            Property="SelectionLength"
            Source="{Binding SelLen}" />
    </i:Interaction.Behaviors>

    
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <b:TextBoxSetStateToSourceAction
                Property="SelectionStart"
                Source="{Binding CursorPos, Mode=OneWayToSource}" />
            <b:TextBoxSetStateToSourceAction
                Property="SelectionLength"
                Source="{Binding SelLen, Mode=OneWayToSource}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

</TextBox>

TextBoxBindingSupportBehavior vs SetState types

For TextBox and PasswordBox, TextBoxBindingSupportBehavior offers a simpler approach: bind SelectedText, SelectionLength, and SelectionStart directly with two-way bindings:

<TextBox>
    <i:Interaction.Behaviors>
        <b:TextBoxBindingSupportBehavior
            SelectedText="{Binding SelectedText}"
            SelectionLength="{Binding SelectionLength}"
            SelectionStart="{Binding SelectionStart}" />
    </i:Interaction.Behaviors>
</TextBox>

Prefer TextBoxBindingSupportBehavior when those three properties are all you need. Use the SetStateTo... types when you need other TextBox-specific state (for example CaretIndex) or want finer control over which events drive updates.

Summary

  • TextBoxSetStateToControlBehavior — add to Behaviors; reflects ViewModel changes on the TextBox (VM → View).
  • TextBoxSetStateToSourceAction — add inside Triggers; sends TextBox state to the ViewModel on events (View → VM).
  • For common two-way binding of SelectedText, SelectionLength, and SelectionStart, TextBoxBindingSupportBehavior is the simpler choice.

Project layout

Path Purpose
src/LiteObservableBehaviors.WPF/Observables/ControlBehavior SetStateToControlBehavior implementations
src/LiteObservableBehaviors.WPF/Observables/SourceAction SetStateToSourceAction trigger actions
src/LiteObservableBehaviors.WPF/Observables/ControlBinding Two-way binding support behaviors
src/LiteObservableBehaviors.WPF/Enhancers Convenience behaviors (focus, drag-and-drop, etc.)
src/LiteObservableBehaviors.WPF/Transitions Panel transition behaviors

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net9.0-windows7.0 is compatible.  net10.0-windows was computed.  net10.0-windows7.0 is compatible. 
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 is compatible.  net481 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 243 7/2/2026