【问题标题】:WPF Custom-Control Style Not Implemented未实现 WPF 自定义控件样式
【发布时间】:2021-10-06 12:35:19
【问题描述】:

希望有人能帮帮我。

我正在通过首先制作一个简单的自定义控件来尝试进入 WPF。结构如下所示。

相应的代码是 MainWindow.xaml

<Window x:Class="WPFCustomControl.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPFCustomControl.NewFolder1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">

<StackPanel Grid.Row="1">
    <local:MyCustomControl x:Name = "customControl"                                  
     Content = "Click Me" Width = "70" 
     Margin = "10" Click = "customControl_Click"/>
    <TextBlock Name = "txtBlock"  
     Width = "250" Height = "30"/>
</StackPanel>

Generic.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFCustomControl.NewFolder1">

<Style TargetType="{x:Type local:MyCustomControl}" BasedOn = "{StaticResource {x:Type Button}}">
    <Setter Property = "Background" Value = "LightSalmon" />
    <Setter Property = "Foreground" Value = "Blue"/>
</Style>

一切都按预期进行。 我的问题是,当我将 Themes 文件夹移动到应用程序编译的 NewFolder1 中但没有实现 Style 时。

提前感谢您提供有关如何解决此问题的任何建议。

【问题讨论】:

    标签: wpf custom-controls


    【解决方案1】:

    我的问题是当我将 Themes 文件夹移动到 NewFolder1 中时,应用程序编译但没有实现 Style。

    这是意料之中的。控件的默认样式应位于themes/generic.xaml。这是约定俗成的,框架会在哪里寻找它。

    您可以将您的Style 移动到另一个ResourceDictionary 中的NewFolder1,然后修改themes/generic.xaml 以合并这个:

    <ResourceDictionary
        ..>
    
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/WpfCustomControl;component/NewFolder1/YourDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    

    或者您可以直接在themes/generic.xaml 中定义您的自定义Style

    【讨论】:

    • 感谢您的回复。如果我要创建一个控件库,在它们的单独文件夹中包含多个控件,每个控件都有自己的样式 ResorceDictionary。他们是否都需要合并到 Themes/Generic.xml 中?
    • @StephenH:是的,没错。
    【解决方案2】:

    很可能您正在犯以下错误, 转到 App.xaml 并添加样式,如下例所示

    <Application.Resources>
            <!-- Resources scoped at the Application level should be defined here. -->
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="NewFolder1/Generic.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
            
        </Application.Resources>
    

    【讨论】:

    • 感谢您的及时回复,但这似乎对我不起作用。一切都可以编译,但在 “查找资源字典主题/Generic.xaml 时发生错误”下出现蓝线错误。我也很好奇为什么当主题文件夹在 NewFolder1 之外时我不需要合并字典,如上图所示。
    • 我应该说我尝试了 。这些都不适合我。
    • 解决方案是我需要包含 /Themes/ 以便它读取 。然而,这确实让我想到了另一个问题。如果我正在创建一个包含多个文件夹的控件库,每个文件夹都有自己独立的主题文件夹,尽管事实上它们只被文件夹中的特定控件使用,这些都需要合并吗?我在上面添加了一张图片来尝试说明我的意思。
    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多