优化样式和功能

This commit is contained in:
2026-02-23 18:53:29 +08:00
parent 18174b404c
commit 68dfdc15a9
4 changed files with 87 additions and 30 deletions

View File

@@ -118,10 +118,13 @@
<Setter Property="BorderThickness" Value="0" />
<Setter Property="assists:ControlAssist.Orientation" Value="Horizontal" />
<Setter Property="assists:ControlAssist.CornerRadius" Value="10" />
<!-- 1. 设置 ItemContainerStyle -->
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="{DynamicResource ControlBackgroundNormalBrush}" />
<Setter Property="Background" Value="Transparent" />
<!-- 改为透明,露出父级背景 -->
<Setter Property="Foreground" Value="{DynamicResource TextPrimaryBrush}" />
<Setter Property="FontSize" Value="16" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
@@ -129,10 +132,11 @@
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<!-- 注意:这里的 CornerRadius 设为 0因为圆角由外层 ListBox 统一裁切 -->
<Border
x:Name="Border"
Background="{TemplateBinding Background}"
CornerRadius="{Binding Path=(assists:ControlAssist.CornerRadius), RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"
CornerRadius="0"
Cursor="Hand">
<ContentPresenter
Margin="10"
@@ -143,8 +147,8 @@
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="{DynamicResource PrimaryNormalBrush}" />
<Setter Property="Background" Value="{DynamicResource ControlBackgroundSelectedBrush}" />
<Setter Property="Foreground" Value="{DynamicResource TextAccentBrush}" />
<Setter Property="Background" Value="{DynamicResource PrimaryNormalBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource TextDisabledBrush}" />
@@ -156,7 +160,8 @@
</Style>
</Setter.Value>
</Setter>
<!-- 设置布局面板为水平排列 -->
<!-- 2. 设置布局面板为水平/垂直排列 -->
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
@@ -164,10 +169,32 @@
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<!-- 3. 设置 ListBox 自身的 Template 进行整体裁切 (核心修改点) -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ItemsPresenter />
<!-- 外层框架:提供整体的背景和真实的圆角 -->
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{Binding Path=(assists:ControlAssist.CornerRadius), RelativeSource={RelativeSource TemplatedParent}}">
<Grid>
<!-- 绘制一个带有圆角的黑色遮罩 Border (颜色不重要OpacityMask只看透明度) -->
<Border
x:Name="Mask"
Background="{DynamicResource ControlBackgroundNormalBrush}"
CornerRadius="{Binding Path=(assists:ControlAssist.CornerRadius), RelativeSource={RelativeSource TemplatedParent}}" />
<!-- ItemsPresenter 展示子项,应用遮罩,超出圆角的部分会被自动裁切 -->
<ItemsPresenter>
<ItemsPresenter.OpacityMask>
<VisualBrush Visual="{Binding ElementName=Mask}" />
</ItemsPresenter.OpacityMask>
</ItemsPresenter>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>

View File

@@ -7,13 +7,16 @@ namespace Melskin.Utilities;
internal class RelayCommand : ICommand
{
private readonly Action<object?> execute;
private readonly Predicate<object?>? _canExecute;
/// <summary>
/// 当命令的可执行状态更改时发生的事件。
/// 此事件通知UI当命令的CanExecute方法返回值发生变化时需要重新查询命令的状态。
/// </summary>
public event EventHandler? CanExecuteChanged;
public event EventHandler? CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
/// <summary>
/// 一个简单的ICommand实现用于传递动作。此命令始终可以执行。
/// </summary>
@@ -24,13 +27,18 @@ internal class RelayCommand : ICommand
/// </summary>
/// <param name="parameter">传递给命令的参数。</param>
/// <returns>返回一个布尔值指示命令是否能够执行。对于此实现总是返回true。</returns>
public bool CanExecute(object? parameter) => true;
public bool CanExecute(object? parameter) => _canExecute == null || _canExecute(parameter);
/// <summary>
/// 执行命令。
/// </summary>
/// <param name="parameter">传递给命令的参数。</param>
public void Execute(object? parameter) => execute(parameter);
/// <summary>
/// 通知命令管理器重新查询此命令的 CanExecute 状态。
/// </summary>
public void RaiseCanExecuteChanged() => CommandManager.InvalidateRequerySuggested();
}
/// <summary>
@@ -66,7 +74,14 @@ internal class RelayCommand<T> : ICommand
/// </summary>
/// <param name="parameter">传递给命令的参数。</param>
/// <returns>返回一个布尔值指示命令是否能够执行。对于此实现总是返回true。</returns>
public bool CanExecute(object? parameter) => canExecute == null || canExecute((T?)parameter);
public bool CanExecute(object? parameter)
{
if (canExecute == null) return true;
// 使用安全的类型获取机制,防止 InvalidCastException
T? validParameter = GetSafeParameter(parameter);
return canExecute(validParameter);
}
/// <summary>
/// 执行命令。
@@ -74,13 +89,26 @@ internal class RelayCommand<T> : ICommand
/// <param name="parameter">传递给命令的参数。</param>
public void Execute(object? parameter)
{
execute((T?)parameter);
T? validParameter = GetSafeParameter(parameter);
execute(validParameter);
}
/// <summary>
/// 通知命令管理器重新查询此命令的CanExecute状态。
/// 此方法用于在命令执行条件可能已更改时更新UI。
/// </summary>
#pragma warning disable CS0067
public void RaiseCanExecuteChanged() => CommandManager.InvalidateRequerySuggested();
/// <summary>
/// 安全地将 object 参数转换为泛型 T。如果类型不匹配则返回 T 的默认值。
/// </summary>
private static T? GetSafeParameter(object? parameter)
{
if (parameter is T tParam)
{
return tParam;
}
// 应对 WPF 绑定时可能会传入 null 的情况,特别是在 T 为值类型时
return default;
}
}

View File

@@ -938,19 +938,23 @@
</ms:FlexibleRowPanel>
<ms:FlexibleRowPanel LayoutMode="Auto" Spacing="20">
<UniformGrid Columns="3">
<ListBox
HorizontalAlignment="Center"
VerticalAlignment="Center"
SelectedIndex="1"
Style="{StaticResource TabGroupListBoxStyle}">
<ListBoxItem Content="Home" />
<ListBoxItem Content="Away" />
<ListBoxItem Content="Night" />
</ListBox>
</UniformGrid>
<ListBox
HorizontalAlignment="Center"
VerticalAlignment="Center"
SelectedIndex="0"
Style="{StaticResource TabGroupListBoxStyle}">
<ListBoxItem Content="Home" />
<ListBoxItem Content="Away" />
<ListBoxItem Content="Night" />
</ListBox>
<ListBox
HorizontalAlignment="Center"
VerticalAlignment="Center"
SelectedIndex="1">
<ListBoxItem Content="Home" />
<ListBoxItem Content="Away" />
<ListBoxItem Content="Night" />
</ListBox>
</ms:FlexibleRowPanel>
</StackPanel>
<StackPanel ms:ControlAssist.AnchorHeaderText="数据展示 TabControl">

View File

@@ -12,13 +12,11 @@ namespace ShrlAlgoToolkit.RevitAddins.Common.Converters;
/// </summary>
public class Rv2WinColorConverter : IValueConverter
{
public static Rv2WinColorConverter Instance { get; } = new Rv2WinColorConverter();
public static readonly Rv2WinColorConverter Instance = new();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var color = (Autodesk.Revit.DB.Color)value;
//if (Autodesk.Revit.DB.SelectedColor.InvalidColorValue)
//{
//}
if (color is { IsValid: true })
{
var rgb = Color.FromRgb(color.Red, color.Green, color.Blue);