新建一个名为"Dictionary1"的资源字典:<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="aa">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border Background="Red">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在项目中使用:<Window x:Class="WPF3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="800" Width="1105">
<Window.Resources>
<Style x:Key="aa">
<Setter Property="Control.Template" >
<Setter.Value>
<ControlTemplate>
<Grid>
<Control Style="{DynamicResource aa}">
<Control.Resources>
<ResourceDictionary Source="pack://application:,,,/WPF3;component/Dictionary1.xaml"/>
</Control.Resources>
</Control>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
</Window>第二段代码为什么会出现这样的错误:无法将类型为“System.Windows.ResourceDictionary”的对象强制转换为类型“Microsoft.Expression.Markup.DocumentModel.DocumentCompositeNode”
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="aa">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border Background="Red">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在项目中使用:<Window x:Class="WPF3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="800" Width="1105">
<Window.Resources>
<Style x:Key="aa">
<Setter Property="Control.Template" >
<Setter.Value>
<ControlTemplate>
<Grid>
<Control Style="{DynamicResource aa}">
<Control.Resources>
<ResourceDictionary Source="pack://application:,,,/WPF3;component/Dictionary1.xaml"/>
</Control.Resources>
</Control>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
</Window>第二段代码为什么会出现这样的错误:无法将类型为“System.Windows.ResourceDictionary”的对象强制转换为类型“Microsoft.Expression.Markup.DocumentModel.DocumentCompositeNode”
解决方案 »
- C# 怎样在for循环中去除DataTable特定的项???
- c#利用正则表达式取出括号中的内容
- 谁设计过这样特别的textbox <winform>
- winform c# access实现备份
- 求救啊,哪位大哥能帮我解决啊,在线等
- 在线求问3:怎么校验文本框里面输入的是数字或者-或者(),最好用同则表达式。并且要求第一位和最后一位必须是数字
- 我调了windows media player 请问如何把它的画片嵌入到程序中来?
- 如何在鼠标点击DataGrid时得到列标题?
- 十万火急,各位大侠救我
- 高手进来看一下,使用内存的问题,,,
- 分享一个数据库匿名对象动态加载
- Entity framework查出来的数据如何传输【为啥没人回帖】
<Application.Resources>
<ResourceDictionary Source="./Dictionary1.xaml"/>
</Application.Resources>在MainWindow里<Window.Resources>
<Style x:Key="aa">
<Setter Property="Control.Template" >
<Setter.Value>
<ControlTemplate>
<Grid>
<Control Style="{DynamicResource aa}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
写在app.xaml里面是很直接的注册方法,因为程序入口就在那个app.xaml,因此所有注册的样式都能有效。
如果想写在其他文件里,并且要让程序能加载它,就必须遵循WPF的规则来写,不能随便命名和存放文件,参考,在Style & FrameworkElement章节里有说明,如果你基础不够,建议看全,这是一篇非常好的文章。