You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

在DLL中配置C# WPF Button自定义样式的技术问询

How to Disable Hover Background Highlight for a Custom Button in Your Reusable DLL

Got it, let's tackle this! Since you're building a reusable DLL to cut down on repetitive code across projects, moving that hover-disabled Button style into your DLL is totally achievable. Here's a step-by-step approach to make it work smoothly:

Step 1: Create a Resource Dictionary in Your DLL

Instead of relying on App.xaml (which is project-specific), you'll store your custom Button style in a standalone ResourceDictionary within your DLL. This keeps the style bundled with your control.

  1. Right-click your DLL project → Add → New Item → Select "Resource Dictionary" (name it something like ButtonCustomStyles.xaml).
  2. Paste your style into the dictionary, modifying it to explicitly disable the hover background highlight. Here's a complete example:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!-- Style to disable hover background highlight -->
    <Style TargetType="{x:Type Button}" x:Key="NoHoverHighlightButtonStyle">
        <Setter Property="Background" Value="#FFFFFF"/> <!-- Default background -->
        <Setter Property="BorderBrush" Value="#CCCCCC"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="buttonBorder"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="True">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Margin="{TemplateBinding Padding}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <!-- Override hover trigger to NOT change background -->
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="buttonBorder" Property="Background" Value="{TemplateBinding Background}"/>
                            <!-- Optional: Keep border highlight if needed, remove if you want no hover changes at all -->
                            <!-- <Setter TargetName="buttonBorder" Property="BorderBrush" Value="#999999"/> -->
                        </Trigger>
                        <!-- Keep other essential triggers for pressed/disabled states -->
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="buttonBorder" Property="Background" Value="#E0E0E0"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="buttonBorder" Property="Opacity" Value="0.5"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

Step 2: Apply the Style to Your Custom Button (Two Options)

You have two ways to connect this style to your Button control in the DLL:

Option 1: Hardcode the Style in Your Custom Button Class

If you've created a custom Button subclass (e.g., MyCustomButton), set the style in the constructor so it's applied automatically whenever the button is used:

using System;
using System.Windows;
using System.Windows.Controls;

namespace YourDLLNamespace
{
    public class MyCustomButton : Button
    {
        public MyCustomButton()
        {
            // Load the resource dictionary from the DLL
            var resourceDict = new ResourceDictionary();
            resourceDict.Source = new Uri("pack://application:,,,/YourDLLName;component/ButtonCustomStyles.xaml", UriKind.RelativeOrAbsolute);
            
            // Assign the style to the button
            this.Style = resourceDict["NoHoverHighlightButtonStyle"] as Style;
        }
    }
}

Replace YourDLLName with your actual DLL project name, and YourDLLNamespace with the namespace where your custom Button lives.

Option 2: Let Consumers Reference the Style

If you want users of your DLL to choose when to apply the style, skip hardcoding and instead have them merge the resource dictionary in their own project's App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- Reference the style dictionary from your DLL -->
            <ResourceDictionary Source="pack://application:,,,/YourDLLName;component/ButtonCustomStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

They can then apply the style to any Button (including your custom one) like this:

<Button Style="{StaticResource NoHoverHighlightButtonStyle}" Content="No Hover Highlight"/>

Key Notes

  • Build Action Check: Ensure your ButtonCustomStyles.xaml file has its Build Action set to Resource (right-click the file → Properties → Build Action).
  • URI Format: The pack://application:,,,/ syntax is critical for referencing resources embedded in a DLL. Double-check the path matches your file's location in the DLL project.
  • Test Thoroughly: Make sure to test the button in a separate project that references your DLL to verify the hover highlight is disabled as expected.

内容的提问来源于stack exchange,提问作者ShadyOverflow

火山引擎 最新活动