Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如果在App.xaml中连接了样式,则按TargetType应用样式_.net_Wpf_Styles_Targettype - Fatal编程技术网

.net 如果在App.xaml中连接了样式,则按TargetType应用样式

.net 如果在App.xaml中连接了样式,则按TargetType应用样式,.net,wpf,styles,targettype,.net,Wpf,Styles,Targettype,例如,我有自己的风格 <Style TargetType="Button"> ... </Style> ... 文件中的Button.xaml 若我在我想要使用它的文件中使用MergeDictionary添加这个样式,那个么一切都很好。但是,如果我将此样式连接到文件层次结构中更高的位置(例如在App.xaml中),样式将停止工作,直到我为其和所有按钮集设置了x:Key Style=“{StaticResource name_of_Style}” 我可以避免这种行为,

例如,我有自己的风格

<Style TargetType="Button">
...
</Style>

...
文件中的Button.xaml 若我在我想要使用它的文件中使用MergeDictionary添加这个样式,那个么一切都很好。但是,如果我将此样式连接到文件层次结构中更高的位置(例如在App.xaml中),样式将停止工作,直到我为其和所有按钮集设置了x:Key

Style=“{StaticResource name_of_Style}”


我可以避免这种行为,还是必须按名称为所有元素设置样式

尝试将其设置为app.xaml中的应用程序资源,如果对视图的UserControl.Resources执行相同操作,则在对特定视图调用特定样式时也可以执行相同操作

PS-示例是silverlight,但方法保持不变

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
              x:Class="The.App">
    <Application.Resources>
        <Style TargetType="Button" BasedOn="{StaticResource name_of_style}"/>
    </Application.Resources>


是的,这很有效,谢谢。但有趣的是,为什么它不起作用?为什么我用这段代码附加样式
你的路径字符串可能不正确,所以它实际上找不到你的资源字典?另外,您的ResourceDictionary集合的类型是No,完全正确,因为我可以按名称使用附加样式。