C# 为什么参考库中的控件使用Window.Resources?

C# 为什么参考库中的控件使用Window.Resources?,c#,wpf,xaml,C#,Wpf,Xaml,我有两个项目,第一个是库,第二个是用户控制 <UserControl x:Class="TestControl.TextTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xml

我有两个项目,第一个是库,第二个是用户控制

<UserControl x:Class="TestControl.TextTest"
         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:TestControl"
         mc:Ignorable="d" 
         d:DesignHeight="150" d:DesignWidth="300">
    <Grid Background="White">
         <TextBlock Text="Test" FontSize="100"/>
    </Grid>
</UserControl>

第二个项目具有定义窗口的窗口。资源和使用来自库的控件

<Window x:Class="WpfTestApp2.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:WpfTestApp2"
    xmlns:TestControl="clr-namespace:TestControl;assembly=TestControl"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" KeyDown="Window_KeyDown">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background" Value="#6b5656"/>
        </Style>
    </Window.Resources>
    <Grid>
         <TestControl:TextTest/>
    </Grid>
</Window>

为什么库中的TextBlock具有其他项目中WindowS.Resources的背景色


是否可以阻止此行为?

这是因为您对所有
TextBlock
元素使用隐式样式:

<Style TargetType="{x:Type TextBlock}">
   <Setter Property="Background" Value="#6b5656"/>
</Style>

这是因为您对所有
TextBlock
元素使用隐式样式:

<Style TargetType="{x:Type TextBlock}">
   <Setter Property="Background" Value="#6b5656"/>
</Style>

由于在
部分中没有
x:key
,资源样式默认应用于窗口中所有适用的内容


是的,可以阻止这种行为。只需添加一个
x:key
,并在希望应用样式的任何位置使用该键。

由于在
部分中没有
x:key
,资源样式默认应用于窗口中的所有适用内容

是的,可以阻止这种行为。只需添加一个
x:key
,并在需要应用样式的任何位置使用该键