簡體   English   中英

Howto:將 generic.xaml 中定義的樣式應用於 UserControl? (WPF)

[英]Howto: Applying a Style defined in generic.xaml to a UserControl? (WPF)

我在 generic.xaml 中創建了一個樣式,我想在我的項目中的幾個 UserControls 上使用它。 以同樣的方式,我為自定義控件定義了一種樣式,並且這個樣式有效,因此它看起來很通用。xaml 已加載,這是定義的樣式:

<Style TargetType="{x:Type UserControl}" x:Key="ServiceStyle" x:Name="ServiceStyle">
    <Setter Property="Opacity" Value="0.5"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type UserControl}">
                <Border Name="border" CornerRadius="20"
                        Margin="10"
                        BorderThickness="5"
                        BorderBrush="Black">
                        <ContentPresenter Content="{TemplateBinding Content}"
                                          Margin="{TemplateBinding Padding}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

但現在我想使用這種風格,但我無法讓它發揮作用。 我嘗試通過以下方式將其作為樣式參數添加到 UserControl 的自定義實例中:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Netcarity"
    xmlns:CustomControls="clr-namespace:Netcarity.CustomControls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Portier_deur" x:Class="Netcarity.UserControlPortier"
    Height="600" Width="800" MouseDown="UserControl_MouseDown" Loaded="UserControl_Loaded" mc:Ignorable="d"
    Style="{StaticResource ServiceStyle}">

但是,這給了我一個提示,即找不到資源 ServiceStyle。 嘗試運行時,會出現運行時錯誤。

提前致謝。

似乎 Generic.xaml 不是為非自定義控件存儲 styles 的正確位置。 在某處我發現將樣式放入 App.xaml 而不是 generic.xaml 的提示,這直接起作用。 所以它接縫Generic.xaml只能用於存儲styles用於customControls。 也許有人可以為這種行為添加一個更有根據的理由?

當 Generic.xaml 使用 ComponentResourceKey。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM