SharpBoxesCore.Wpf 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SharpBoxesCore.Wpf --version 1.0.0
                    
NuGet\Install-Package SharpBoxesCore.Wpf -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="SharpBoxesCore.Wpf" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SharpBoxesCore.Wpf" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SharpBoxesCore.Wpf" />
                    
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 SharpBoxesCore.Wpf --version 1.0.0
                    
#r "nuget: SharpBoxesCore.Wpf, 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 SharpBoxesCore.Wpf@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=SharpBoxesCore.Wpf&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SharpBoxesCore.Wpf&version=1.0.0
                    
Install as a Cake Tool

📦 SharpBoxesCore.Wpf

🔧 为 WPF MVVM 开发者打造的轻量级工具库
🌟 支持只读属性绑定、行为扩展、属性编辑控件
🚀 简洁 · 高效 · 无代码隐藏


📖 简介

SharpBoxesCore.Wpf 是一个专为 WPF + MVVM 架构设计的实用控件与扩展库。它解决了传统 WPF 开发中常见的痛点,如:

  • 无法监听只读依赖属性(如 VerticalOffset
  • 缺少通用的属性编辑控件
  • MVVM 中 UI 状态同步复杂

该库提供三大核心组件,帮助你构建更干净、更灵活、更可维护的 WPF 应用。


🧩 核心组件概览

组件 类型 功能
PropertyGrid 控件 可视化编辑对象属性,支持分类、排序、类型智能识别
ReadOnlyBinding 附加属性 声明式监听只读 DP,支持命令、转换、过滤
BindReadOnlyPropertyBehavior Behavior 行为式监听只读 DP,适用于复杂逻辑封装

🔧 详细功能说明

1. 🎛️ PropertyGrid 控件

一个功能完整的属性编辑器,支持运行时查看和修改对象属性。

✅ 功能特性
特性 说明
按分类分组 使用 [Category("UI")] 自动分组显示
自定义排序 使用 [PropertyOrder(1)] 控制显示顺序
智能编辑器 根据属性类型自动选择编辑控件(文本、数字、布尔、枚举等)
描述显示 显示 [Description("...")] 注释信息
双向绑定 属性修改实时同步到数据源
💡 使用示例
<Window xmlns:pg="http://schemas.sharpboxes.com/wpf/propertygrid">
    <pg:PropertyGrid 
        SelectedObject="{Binding SelectedItem}" 
        Margin="10" />
</Window>
📝 支持的属性特性
[Category("外观")]
[DisplayName("边框颜色")]
[Description("设置控件边框颜色")]
[PropertyOrder(1)]
public Color BorderColor { get; set; }

2. 🔗 ReadOnlyBinding 附加属性

一种声明式、简洁、现代化的方式,用于监听只读依赖属性的变化。

✅ 核心能力
  • ✅ 监听任意 DependencyProperty(包括只读)
  • ✅ 同步值到 ViewModel 属性
  • ✅ 触发 ICommandIAsyncCommand
  • ✅ 支持 IValueConverter 转换
  • ✅ 条件过滤:Predicate<object>
  • ✅ 自动 await IAsyncCommand(兼容 CommunityToolkit.MVVM
💡 使用示例
<ScrollViewer
    local:ReadOnlyBinding.Property="VerticalOffset"
    local:ReadOnlyBinding.TargetPropertyName="CurrentScroll"
    local:ReadOnlyBinding.Command="{Binding ScrollCommand}"
    local:ReadOnlyBinding.Converter="{StaticResource OffsetToText}"
    local:ReadOnlyBinding.Filter="{Binding ShouldNotifyFilter}" />
🧩 绑定流程
UI 属性变化 → (可选过滤) → (可选转换) → 更新 ViewModel 属性 + 执行命令

3. 🤖 BindReadOnlyPropertyBehavior 行为

一种面向对象、可复用的方式,用于监听只读属性变化,适合需要封装复杂逻辑的场景。

✅ 功能特性
功能 说明
更新 ViewModel 属性 通过 TargetPropertyName
执行 ICommand 支持参数传递
值转换 支持 IValueConverter
条件过滤 使用 Predicate<object> 控制触发时机
💡 使用示例
<ScrollViewer x:Name="MyScroll" Height="300">
    <i:Interaction.Behaviors>
        <local:BindReadOnlyPropertyBehavior 
            Property="{x:Static ScrollViewer.VerticalOffsetProperty}"
            TargetPropertyName="CurrentScrollOffset"
            Command="{Binding ScrollCommand}"
            Converter="{StaticResource MyConverter}"
            Filter="{Binding LargeScrollFilter}" />
    </i:Interaction.Behaviors>
</ScrollViewer>
⚖️ ReadOnlyBinding vs Behavior
对比项 ReadOnlyBinding BindReadOnlyPropertyBehavior
语法 简洁(附加属性) 稍冗长(Behavior)
可读性 ⭐️⭐️⭐️⭐️⭐️ ⭐️⭐️⭐️⭐️
扩展性 中等 高(可添加方法、事件)
推荐场景 快速绑定 复杂逻辑封装

🎯 典型使用场景

场景 推荐组件
展示并编辑对象属性(如设置面板) PropertyGrid
监听 ScrollViewer.VerticalOffset ReadOnlyBindingBehavior
同步 TextBox.Text 到 ViewModel ReadOnlyBinding
响应 ListBox.SelectedItem 变化 ReadOnlyBinding.Property="SelectedItem"
滚动到底部自动加载更多 ReadOnlyBinding + IAsyncCommand

⚙️ 依赖项

<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />

💡 CommunityToolkit.Mvvm 用于 IAsyncCommand 支持
💡 Microsoft.Xaml.Behaviors.Wpf 用于 Behavior 支持


🖥️ 支持的框架

框架 支持状态
.NET Framework 4.8 ✅ 支持
.NET 6.0 (Windows) ✅ 支持
.NET 7/8 ✅ 兼容(需指定 TargetFrameworknet6.0-windows

🧪 完整示例

ViewModel

[ObservableProperty]
private double _currentScroll;

[RelayCommand]
private async Task ScrollCommand(double offset)
{
    await Task.Delay(100);
    Debug.WriteLine($"滚动到: {offset}");
}

public Predicate<object> LargeScrollFilter => v => v is double d && d > 100;

XAML

<Window 
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:local="clr-namespace:SharpBoxesCore.Wpf">

    
    <ScrollViewer
        local:ReadOnlyBinding.Property="VerticalOffset"
        local:ReadOnlyBinding.TargetPropertyName="CurrentScroll"
        local:ReadOnlyBinding.Command="{Binding ScrollCommand}" />

    
    <ScrollViewer x:Name="MyScroll">
        <i:Interaction.Behaviors>
            <local:BindReadOnlyPropertyBehavior 
                Property="{x:Static ScrollViewer.VerticalOffsetProperty}"
                TargetPropertyName="CurrentScroll"
                Command="{Binding ScrollCommand}" />
        </i:Interaction.Behaviors>
    </ScrollViewer>

    
    <pg:PropertyGrid SelectedObject="{Binding SelectedItem}" />

</Window>

📚 最佳实践

  • ✅ 优先使用 ReadOnlyBinding 实现简单绑定
  • ✅ 在复杂逻辑中使用 Behavior
  • ✅ 为 PropertyGrid 的属性添加 CategoryDescription
  • ✅ 使用 IAsyncCommand 避免 UI 冻结
  • ✅ 在 OnDetaching 中清理监听,防止内存泄漏(库已自动处理)

📎 参考资料


📬 反馈与贡献

欢迎提交 Issue 或 Pull Request!

  • GitHub: https://github.com/yourusername/SharpBoxesCore.Wpf
  • 邮箱: support@sharpboxes.com

© 2025 SharpBoxes. All rights reserved.
Made with ❤️ for WPF developers.


✅ 一句话总结

SharpBoxesCore.Wpf = 属性编辑 + 只读绑定 + MVVM 友好 = 更高效的 WPF 开发体验


Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed.  net9.0-windows was computed.  net10.0-windows was computed. 
.NET Framework net48 is compatible.  net481 was computed. 
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.1.2 113 9/3/2025
1.1.1 169 8/27/2025
1.1.0 118 8/22/2025
1.0.0 103 8/9/2025