![](/img/trans.png)
[英]WPF Parent UserControl cannot locate resource, thats beeing used in child 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.