【问题标题】:C# WPF Button Styles inside DLLDLL 中的 C# WPF 按钮样式
【发布时间】:2018-07-14 00:11:00
【问题描述】:

我正在为我制作一个 DLL,以减轻我的工作,因为我在每个项目中都会使用一些类,所以我为什么要复制它们,当我可以使用一个 DLL 来完成工作时,

我还想给它添加一些控件,按钮,所以它是这样的:

我创建了一个按钮,效果很好,但是我想给它添加一个自定义样式,当你鼠标悬停时禁用背景突出显示,现在我以前使用过这个样式并且效果很好,但是在以前有时,我会将样式添加到 app.xaml 资源中,然后将样式设置为按钮,例如:

Style="{StaticResource DisableBackgroundHighlight}"

但是由于DLL没有app.xaml,我该怎么办,怎么给DLL里面的控件添加样式呢? 我在谷歌上找到的只是将 DLL 中的资源引用到 WPF 应用程序的 app.xaml 中,但这不是我想要的, 我试过这个:

<Button x:Class="SRX.Windows.Controls.SRXButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:SRX.Windows.Controls"
         mc:Ignorable="d" 
         d:DesignHeight="35" d:DesignWidth="100" Content="OK" Background="White" BorderBrush="Blue" Foreground="Blue" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Style="{StaticResource DisableBackgroundHighlight}">
<Button.Resources>
    <Style x:Key="DisableBackgroundHighlight" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="border" BorderThickness="0" BorderBrush="Black" Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Opacity" Value="0.8" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Button.Resources>

但它不起作用,它显示“资源“DisableBackgroundHighlight”无法解析。”虽然它编译但在启动时崩溃。 如果我在问题说明中遗漏了什么,请让我解决,提前感谢。

【问题讨论】:

    标签: c# wpf xaml dll styles


    【解决方案1】:

    只需将 xaml 文件添加到您的项目中。我们称它为 Generic.xaml,它通常是您的自定义 coltrols 的模板所在的位置。

    此文件将具有以下格式:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        x:Class="Your.Domain.Generic">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="whatever else you defined in another xaml"/>        
        </ResourceDictionary.MergedDictionaries>
        <Style TargetType="TextBox" ....
    </ResourceDictionary>
    

    在其他程序集上,您可以像导入其他任何程序一样导入“样式”程序集:

    xmlns:style="clr-namespace:Your.Domain.Shared"
    

    当然假设您的样式集名为 Your.Domain.Shared

    【讨论】:

      猜你喜欢
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多