簡體   English   中英

WPF無法找到內部UserControl樣式

[英]WPF cannot locate internal UserControl style

我有一個自定義UserControl ,我嘗試將其添加到另一個UserControl

<UserControl x:Class="MyProject.Screens.Test"
         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:MyProject.Screens"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Button Style="{StaticResource TestStyle}"/>
</Grid>
<UserControl.Resources>
    <Style x:Key="TestStyle" TargetType="{x:Type Button}">
        <Setter Property="Padding" Value="1"/>
    </Style>
</UserControl.Resources>

在設計窗口中看起來還可以,並且項目可以正常編譯。 但是,如果我嘗試將此UserControl添加到另一個UserControl

<UserControl x:Class="MyProject.Screens.MainScreen"
         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:MyProject.Screens"
         mc:Ignorable="d" 
         d:DesignHeight="1080" d:DesignWidth="1920">
  <Grid>
    <local:Test/>
  </Grid>
</UserControl>

我收到一個錯誤:

找不到資源“ TestStyle”

MainScreen 我究竟做錯了什么?

將資源聲明放在內容之前:

<UserControl ...>
    <UserControl.Resources>
        <Style x:Key="TestStyle" TargetType="{x:Type Button}">
            <Setter Property="Padding" Value="1"/>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Button Style="{StaticResource TestStyle}"/>
    </Grid>
</UserControl>

暫無
暫無

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

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